zoukankan      html  css  js  c++  java
  • 对象内存布局 (8)

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

    #include <iostream>
    using namespace std;
    
    class Base1
    {
    public:
        int m_base1;
        inline virtual void vfBase1_1()
        {
            cout << "This is in Base1::vfBase1_1()" << endl;
        }
        inline virtual void vfBase1_2()
        {
            cout << "This is in Base1::vfBase1_2()" << endl;
        }
    };
    class Base2
    {
    public:
        int m_base2;
        inline virtual void vfBase2_1()
        {
            cout << "This is in Base2::vfBase2_1()" << endl;
        }
        inline virtual void vfBase2_2()
        {
            cout << "This is in Base2::vfBase2_2()" << endl;
        }
    };
    class Base3
    {
    public:
        int m_Base3;
        inline virtual void vfBase3_1()
        {
            cout << "This is in Base3::vfBase3_1()" << endl;
        }
        inline virtual void vfBase3_2()
        {
            cout << "This is in Base3::vfBase3_2()" << endl;
        }
    };
    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;
    
        }
    };
    typedef void (*VFun)(void);
    template<typename T>
    VFun virtualFunctionPointer(T* b, int i)
    {
        return (VFun)(*((int*)(*(int*)b) + i));
    }
    int main(void)
    {
        Derived d;
        cout << "The size of Derived object = 	" << sizeof(Derived) << endl;
        cout << endl;
        cout << "1st virtual function table: " << endl;
        int i = 0;
        while(virtualFunctionPointer(&d, i)&&i<3)
        {
            VFun pVF = virtualFunctionPointer(&d, i++);
            pVF();
        }
        cout << endl;
        cout << "2nd virtual function table: " << endl;
        i = 0;
        //以32字长的机器,找到下一个继承base class的vptr
        int* tmp = ((int*)&d)+sizeof(Base1)/4;
        //虚函数表中的虚函数后面不为NULL?如果不加i的限制会出现段错误,不能结束循环
        while(virtualFunctionPointer(tmp, i)&&i<2)
    
        {
            VFun pVF = virtualFunctionPointer(tmp, i++);
            pVF();
        }
        cout << endl;
        cout << "3rd virtual function table: " << endl;
        i = 0;
        tmp = ((int*)&d) + sizeof(Base2)/4;
        while(virtualFunctionPointer(tmp, i)&&i<2)
        {
            VFun pVF = virtualFunctionPointer(tmp, i++);
            pVF();
        }
        return 0;
    }

    运行结果:

    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中自己定义的虚函数,总是处在第一个虚函数表的最后一项的位置。

  • 相关阅读:
    odoo开发笔记 -- 异常、错误、警告、提示、确认信息显示
    odoo开发笔记--前端搜索视图--按照时间条件筛选
    odoo开发笔记-自定义发送邮件模板
    html表格导出Excel的一点经验心得
    throw和throw ex的区别
    js中对String去空格
    根据不同的多语言环境来切换不同的页面样式的具体示例
    HTML中 :after和:before的作用及使用方法(转)
    CSS清除浮动方法集合
    页面的Tab选项卡 简单实例
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4091524.html
Copyright © 2011-2022 走看看