对象内存布局 (8)

转自http://blog.csdn.net/pathuang68/article/details/4102006


内存对象布局 (5)的代码中,在Derived类中将三个基类中的虚函数分别覆盖一个,即分别覆盖Base1中声明的vfBase1_1(),Base2中声明的vfBase2_1()以及Base3中声明的vfBase3_1()。保持其他代码不变,修改后的Derived代码如下:

class Derived : public Base1, public Base2, public Base3

{

public:

         int m_derived;

         inline virtual void fd()

         {

                   cout << "This is in Derived::fd()" << endl;

         }

         inline void vfBase1_1()

         {

                   cout << "This is in Derived::vfBase1_1()" << endl;

         }

         inline void vfBase2_1()

         {

                   cout << "This is in Derived::vfBase2_1()" << endl;

         }

         inline void vfBase3_1()

         {

                   cout << "This is in Derived::vfBase3_1()" << endl;

         }

};

运行结果如下:

 

Derived对象的memory layout图解如下:

 

Derived中覆盖的虚函数,分别出现在三个不同的虚函数表中,而且分别代替个基类的原虚函数的位置,即:

第一个虚函数表中,Derived::vfBase1_1()代替了Base::vfBase1_1()的位置,Base::vfBase1_1()不再在虚函数表中出现;

第二个虚函数表中,Derived::vfBase2_1()代替了Base::vfBase2_1()的位置,Base::vfBase2_1()不再在虚函数表中出现;

第三个虚函数表中,Derived::vfBase3_1()代替了Base::vfBase3_1()的位置,Base::vfBase3_1()不再在虚函数表中出现;

在Derived中自己定义的虚函数,总是处在第一个虚函数表的最后一项的位置。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值