I searched this article: C++ : Getting function virtual 'address' with member function pointer
In order to test if the virtual member function is usually at the beginning address of the object, I wrote the code as following:
#include <pwd.h>
#include <string.h>
#include <stdio.h>
class Base
{
public:
int mBase1;
char mBase2;
virtual void foo()
{
fprintf(stderr,"Base foo called\n");
}
};
class Child: public Base
{
public:
int cc;
virtual void foo()
{
fprintf(stderr,"Child foo called");
}
};
int main(int argc, char *argv[])
{
Base bb;
fprintf(stderr,"[mBase1 %p][mBase2 %p] [foo %p]\n",&bb.mBase1,&bb.mBase2,&Base::foo);
return 0;
}
when compiling, I got a warning:
test.cpp:30:88: warning ‘%p’ expects argument of type ‘void*’, but argument 5 has type ‘void (Base::*)()’ [-Wformat]
The output is:
[mBase1 0xbfc2ca38][mBase2 0xbfc2ca3c] [foo 0x1]
I consider it's wired.
Is there any "pretty method" to get the member function (not static member function address?). Besides the following method, is there any other elegant way?
typedef void (Base::*Foo)(); Foo f = &Base::foo();
Why doesn't
&Base::foo
get the correct C++ member address?