函数指针和类成员函数指针的问题,新新新,欢迎大家继续讨论
在http://www.csdn.net/expert/topic/189/189742.shtm
我问过怎么转化个类的方法赋给一个普通的函数指针?
最后似乎得到无解的答案
但今天看帖子http://www.csdn.net/expert/Topic/193/193682.shtm,
我找到答案,这是有解的~~~~~~~
class A;
void __stdcall G_Fun(A* p)
{
cout<<"G_Fun(A*)"<<endl;
};
class A
{
public:
int x;
int c;
virtual void __stdcall fun();
virtual void __stdcall fun1();
virtual void __stdcall fun2();
};
void __stdcall A::fun()
{
cout<<"A::fun()"<<endl;
}
void __stdcall A::fun1()
{
cout<<"A::fun1()"<<endl;
}
void __stdcall A::fun2()
{
cout<<"A::fun2()"<<endl;
}
int main()
{
void __stdcall (*xxx)(A*);
A* pA = new A ;
(int )xxx=(**(int**)pA); //转化第一个成员方法
xxx(pA);
(int )xxx=(*(*(int**)pA+1)); //转化第二个成员方法
xxx(pA);
(int )xxx=(*(*(int**)pA+2)); //转化第三个成员方法
xxx(pA);
cout<<"first,A::fun()"<<endl;
pA->fun();
//now let we modified the vtable.
(**(int**)pA)=(int)G_Fun;;
cout<<"second,G_Fun()"<<endl;
pA->fun();
delete pA;
return 0 ;
}