非静态成员函数地址

1,使用printf输出,只能使用printf。cout <<输出的是bool值:1

  如果是非静态的不能输出地址 而是bool值,cout<<并没有对void(__thiscall A::*)()类型重载,编译器将这种类型转换为bool类型, 静态函数并非__thiscall,cout<<有对它的重载,因此类的静态函数可以直接用cout输出函数地址。 可以用printf输出,因为他可以接收任意类型的参数,包括__thiscall类型
  // %p表示显示一个指针

例子:

class VirtualTest { public: int intSlot; float floatSlot; bool boolSlot1; bool boolSlot2; void test0() { cout << "Test0" << endl; } virtual void test1() { cout << "Test1" << endl; } virtual void test2() { cout << "test2" << endl; } static void staticTest() { } };

//cout << &VirtualTest::test0 << endl; // get : 1 printf("%d/n", &VirtualTest::test0); printf("%p/n", &VirtualTest::test1); printf("%p/n", &VirtualTest::test2); cout << &VirtualTest::staticTest << endl;

2,将函数地址转化为整数,然后输出。

直接DWORD dwAddrPtr = &VirtualTest::test0; 编译通不过的。

template <typename ToType, typename FromType> void GetMemberFuncAddr_VC6(ToType& addr,FromType f) { union { FromType _f; ToType _t; }ut; ut._f = f; addr = ut._t; }

typedef unsigned long DWORD; DWORD dwAddrPtr; GetMemberFuncAddr_VC6(dwAddrPtr, &VirtualTest::test0); cout << dwAddrPtr << endl; GetMemberFuncAddr_VC6(dwAddrPtr, &VirtualTest::test1); cout << dwAddrPtr << endl; GetMemberFuncAddr_VC6(dwAddrPtr, &VirtualTest::test2); cout << dwAddrPtr << endl;

你可能感兴趣的:(Class,编译器)