类的数据成员指针及类的成员函数指针
代码如下:
struct MemberPointer {
int m_a;
char* getname(int parama) const;
MemberPointer() {
m_a = 0;
}
};
char* MemberPointer::getname(int parama) const {
return "hello, my name is member pointer";
}
int _tmain(int argc, _TCHAR* argv[])
{
MemberPointer mp;
MemberPointer *pmp = new MemberPointer;
//指向类成员函数的指针
char* (MemberPointer::*pmf)(int parama) const = &MemberPointer::getname;
char* ca = (mp.*pmf)(3);
char* cb = (pmp->*pmf)(4);
std::cout << ca << std::endl;
std::cout << cb << std::endl;
//指向类数据成员的指针
int MemberPointer:: *pmd = &MemberPointer::m_a;
mp.*pmd = 9;
pmp->*pmd = 12;
std::cout << mp.*pmd << std::endl;
std::cout << pmp->*pmd << std::endl;
delete pmp;
}