1. First introduce the memory structure of C++ class, you can read the following blog, feel good
https://blog.csdn.net/fenxinzi557/article/details/51995911
The knowledge point related to this time is that the first 4 bytes of a class with virtual functions are pointers to the first address of the virtual function table _vfptr
2. Let's start 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 intmain() { AA a; // *****printf("Virtual table address:%p\n", *(int *)&a); Parse *****: // 1.&a represents the starting address of object a // 2. (int *)&a is cast to int * type, in order to take the first four bytes of the a object later, the first four bytes are the virtual table pointer // 3.*(int *)&a Take the first four bytes, which is the address of the vptr virtual table // *****printf("First virtual function address: %p\n", *(int *)*(int *)&a);*****: // According to the above analysis, we know that *(int *)&a is vfptr, the virtual table pointer. And the virtual table stores virtual function pointers // So each element (virtual function pointer) in the virtual table is 4 bytes under a 32-bit compiler, so (int *)*(int *)&a // After such a strong conversion, four bytes are taken for the following. So *(int *)*(int *)&a is the first element of the virtual table. // That is the address of f(). // Then take the second virtual function address and so on. Always remember that vfptr points to a piece of memory, // This piece of memory stores the virtual function address, this piece of memory is what we call the virtual table. // printf("Address of virtual table:%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); pounds(); pfun = (Fun)(*((int *)*(int *)(&a) + 1)); //virtual func2(); printf("func2():%p\n", pfun); pounds(); }