---------begin base object inner memory layout test------- object size is: 16 objectaddris: 0x7ffd9a7680e0 object vfptr value is: 0x559bc73a9ce0 typeid(b) is: 4Base object vfptr[0] funcptris: 0x559bc73a611a object vfptr[0] func invoke res: BaseClassRun() object vfptr[1] funcptris: 0x559bc73a6156 object vfptr[1] func invoke res: BaseClassRunBase() object _base_var addris: 0x7ffd9a7680e8 object _base_var value is: 10 ---------end base object inner memory layout test-------
3.内存布局示意图:
普通单继承的子类
简单实现代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Single Common Derive: Second Inheritance Level classDerive_Sin_Com : public Base { public: Derive_Sin_Com(int var = 20) : Base(var), _derive_sin_com_var(var) {} public: virtualvoidRun(void)// override Base::Run() { std::cout << "Derive_Sin_Com Class Run()" << std::endl; } virtualvoidRunDerive_Sin_Com(void)// new virtual function { std::cout << "Derive_Sin_Com Class RunDerive_Sin_Com()" << std::endl; } private: int _derive_sin_com_var; };
Gcc查看内存布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Vtable for Derive_Sin_Com Derive_Sin_Com::_ZTV14Derive_Sin_Com: 5 entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI14Derive_Sin_Com) 16 (int (*)(...))Derive_Sin_Com::Run 24 (int (*)(...))Base::RunBase 32 (int (*)(...))Derive_Sin_Com::RunDerive_Sin_Com Class Derive_Sin_Com size=16 align=8 base size=16 base align=8 Derive_Sin_Com (0x0x7f223ab202d8) 0 vptr=((& Derive_Sin_Com::_ZTV14Derive_Sin_Com) + 16) Base (0x0x7f223ab13480) 0 primary-for Derive_Sin_Com (0x0x7f223ab202d8)
---------begin single common derive object inner memory layout test------- object size is: 16 object addr is: 0x7ffd9db3df50 object vfptr value is: 0x5606ab76b728 typeid(d) is: 14Derive_Sin_Com object vfptr[0] func ptr is: 0x5606ab7671c8 object vfptr[0] func invoke res: Derive_Sin_Com Class Run() object vfptr[1] func ptr is: 0x5606ab766d62 object vfptr[1] func invoke res: Base Class RunBase() object vfptr[2] func ptr is: 0x5606ab767204 object vfptr[2] func invoke res: Derive_Sin_Com Class RunDerive_Sin_Com() object _base_var addr is: 0x7ffd9db3df58 object _base_var value is: 20 object _derive_sin_com_var addr is: 0x7ffd9db3df5c object _derive_sin_com_var value is: 20 ---------end single common derive object inner memory layout test-------
---------begin multi common derive object inner memory layout test------- object size is: 32 object addr is: 0x7ffdcf415720 typeid(d) is: 14Derive_Mul_Com ------ base obj begin ----- object base vfptr value is: 0x558b051786e0 object base vfptr[0] func ptr is: 0x558b051742a4 object base vfptr[0] func invoke res: Derive_Mul_Com Class Run() object base vfptr[1] func ptr is: 0x558b051742ea object base vfptr[1] func invoke res: Derive_Mul_Com Class RunBase() object base vfptr[2] func ptr is: 0x558b05174326 object base vfptr[2] func invoke res: Derive_Mul_Com Class RunDerive_Mul_Com() object _base_var addr is: 0x7ffdcf415728 object _base_var value is: 30 ------ base obj end ----- ------ base2 obj begin ----- object base2 vfptr addr is: 0x7ffdcf415730 object base2 vfptr value is: 0x558b05178708 object base2 vfptr[0] func ptr is: 0x558b051742df object base2 vfptr[0] func invoke res: Derive_Mul_Com Class Run() object base2 vfptr[1] func ptr is: 0x558b05173e04 object base2 vfptr[1] func invoke res: Base2 Class RunBase2() object _base2_var addr is: 0x7ffdcf415738 object _base2_var value is: 30 ------ base2 obj end ----- object _derive_mul_com_var addr is: 0x7ffdcf41573c object _derive_mul_com_var value is: 30 ---------end multi common derive object inner memory layout test-------