1. First introduce the C + + class memory structure, we can see the following blog, feel good
51995911
Where and this related knowledge point is a virtual function of the first 4 bytes of the class is a pointer to the first address of the virtual function _vfptr
2. The following starts with the specific solution process
Class AA {public:virtual void func1 () {cout << "AA:: Func1" << Endl;} virtual void Func2 () {cout << "AA:: Func2" << Endl;} void Func3 () {cout << "aa::func3" << Endl;} }; typedef void (*fun) (void); function pointer int main () {AA A; printf ("Virtual table Address:%p\n", * (int *) &a); * * * * * *://1.&a represents the starting address of object A//2. (int *) &a strong to int * type, in order to take the first four bytes of a object, the first four bytes is the virtual table pointer//3.* (int *) &a take the first four bytes, that is vptr the virtual table address//*****printf (" The first virtual function address:%p\n ", * (int *) * (int *) &a); * * * * * * * * * * * (int *) &a is vfptr, that is, virtual table pointer. and virtual table is a virtual function pointer///SO virtual table Each element (virtual function pointer) is 4 bytes under the 32-bit compiler, so (int *) * (int *) &a//Such a strong turn to fetch four bytes later. so * (int *) * (int *) &a is the first element of a virtual table. That is, the address of f (). Then the second virtual function address is followed and so on. Always remember that vfptr points to a piece of memory,//This memory holds the virtual function address, this memory is what we call the virtual table. printf ("Virtual table Address:%p\n", * (int *) &a); printf ("First virtual function Address:%p\n", * (int *) * (int *) &a); printf ("Second virtual function address:%p\n", * (int *) * (inT *) (&A) + 1); Fun Pfun = (fun) * ((int *) * (int *) (&a)); Virtual func1 (); printf ("Func1 ():%p\n", pfun); Pfun (); Pfun = (Fun) (* (int *) * (int *) (&a) + 1)); Virtual Func2 (); printf ("Func2 ():%p\n", pfun); Pfun (); }
Get C + + virtual function table address and virtual function address