#include <iostream> using namespace std; class Ctest { public: static void statFunc() { cout << "statFunc" << endl; } void dynFunc() { cout << "dynFunc" << endl; } virtual void virtFunc() { cout << "virtFunc" << endl; } }; int main() { Ctest Object; cout << "address of Ctest::statFunc:" << &Ctest::statFunc << endl; cout << "address of Ctest::dynFunc :" << &Ctest::dynFunc << endl; cout << "address of Ctest::virtFunc:" << &Ctest::virtFunc << endl; static void (*p_statFunc)(); void (Ctest::*p_dynFunc)(); void (Ctest::*p_virtFunc)(); p_statFunc = &Ctest::statFunc; p_dynFunc = &Ctest::dynFunc; p_virtFunc = &Ctest::virtFunc; p_statFunc(); (Object.*p_dynFunc)(); (Object.*p_virtFunc)(); return 0; }
用union逃避类型检查
#include <iostream> using namespace std; class A{ public: void fun() { cout << "Hi" << endl; } }; template <class ToType, class FromType> void GetMemberFuncAddr_VC6(ToType& addr,FromType f) { union { FromType _f; ToType _t; }ut; ut._f = f; addr = ut._t; } int main() { printf("00%X ", &A::fun); void *x; GetMemberFuncAddr_VC6(x, &A::fun); cout << x << endl; return 0; }
#include <iostream> using namespace std; #define GetMemberFuncAddr_VC8(FuncAddr, FuncType) { __asm { mov eax,offset FuncType }; __asm { mov FuncAddr, eax }; } class A{ public: void fun() { cout << "Hi" << endl; } }; template <class ToType, class FromType> void GetMemberFuncAddr_VC6(ToType& addr,FromType f) { union { FromType _f; ToType _t; }ut; ut._f = f; addr = ut._t; } int main() { void *x; GetMemberFuncAddr_VC6(x, &A::fun); cout << x << endl; GetMemberFuncAddr_VC8(x, A::fun); cout << x << endl; return 0; }
http://www.vckbase.com/index.php/wv/1514