《c++ 对象模型》讲到了,对于虚函数表指针的分布,直接截书中的图
先上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| struct no_virts { int d1; int d2; };
class has_virts : public no_virts { public: int d3; virtual void foo() { printf("--- has_virts foo \n"); } };
void testMemObj() { has_virts hv; hv.d1 = 111; hv.d2 = 222; hv.d3 = 333;
Fun pFunc = (Fun)*((int*)(*((int*)(&hv) + 0)) + 0); pFunc();
int d1 = (int)*((int*)(&hv) + 1); printf("--- d1:%d\n", d1); int d2 = (int)*((int*)(&hv) + 2); printf("--- d2:%d\n", d2); int d3 = (int)*((int*)(&hv) + 3); printf("--- d3:%d\n", d3); }
|
