class AbstractMethod
{
public:
virtual void show(){} // = 0; // 可以是纯虚函数,这里为了测试方便不使用纯虚函数!
{
public:
virtual void show(){} // = 0; // 可以是纯虚函数,这里为了测试方便不使用纯虚函数!
void fun()
{
cout << "I was called!" << endl;
}
void fun1()
{
cout << "I was called!" << endl;
}
};
int main()
{
// 定义成员函数指针类型
typedef void (AbstractMethod::*MFP)(void);
// 转化函数指针为别的指针
MFP mfp1 = &AbstractMethod::show;
unsigned* tmp = (unsigned*)&mfp1;
cout << hex << *tmp << endl;
MFP mfp2 = &AbstractMethod::fun;
tmp = (unsigned*)&mfp2;
cout << hex << *tmp << endl;
MFP mfp3 = &AbstractMethod::fun1;
tmp = (unsigned*)&mfp3;
cout << hex << *tmp << endl;
// 通过转化后的指针调用成员函数
AbstractMethod am;
MFP* addr = (MFP*)tmp;
(am.*mfp3)();
(am.*(*addr))();
return 0;
}