• C++内存布局


      一直想写一篇有关C++内存布局的文章,结合编译器VS2010来探讨C++对象模型在内存中的表现形式,主要是自己看《深度探索C++对象模型》太枯燥了,而且这本书也是较早出的,讨论的编译器都差不多过时了,所以才有这个想法,希望看官勿喷。废话少说,let's go...

     

    没有虚函数的单继承

      下面,我们假设有如下所示的单继承关系:

                             

      请注意,在这个继承关系中,父类,子类,都有自己的成员变量。而子类仅仅覆盖了父类的f()函数

      源代码如下:

    class Base
    {
    public:
        void f(){ cout<<"Base::f()"<<endl; }
        void g(){ cout<<"Base::g()"<<endl; }
        void h(){ cout<<"Base::h()"<<endl; }
    private:
        char chBase;
        int  iBase;
    };
    
    class Derive : public Base
    {
    public:
        void f() { cout<<"Derive::f()"<<endl; }
        void g1(){ cout<<"Derive::g()"<<endl; }
        void h1(){ cout<<"Derive::h()"<<endl; }
    private:
        char chDerive;
        int  iDerive;
    };

      这是没有虚函数的单继承,子类Derive的f()函数会覆盖父类Base的f()函数,当使用父类指针指向子类对象,并调用f()函数时,实际调用的是父类的f()函数。因为f()函数不是虚拟函数,编译器在编译的时候就确定声明什么类型的指针就调用什么类型的函数。很简单,这里就不举测试程序了。我们查看VC编译时,输出的信息可以看到父类和子类在内存中的布局如下:(注:这是VS2010输出的信息,下文输出这些类型信息也是)

    1>class Base size(8):
    1> +---
    1> 0 | chBase
    1>   | <alignment member> (size=3)
    1> 4 | iBase
    1> +---
    1>class Derive size(16):
    1> +---
    1> | +--- (base class Base)
    1> 0 | | chBase
    1>   | | <alignment member> (size=3)
    1> 4 | | iBase
    1> | +---
    1> 8 | chDerive
    1>   | <alignment member> (size=3)
    1>12 | iDerive

      可以看到:

        1、成员变量根据其继承和声明顺序依次放在后面

        2、成员函数定义在类体之外

        3、调整字节对齐使bus的“运输量”达到最高效率

    没有虚函数的多重继承

      下面,我们假设有如下所示的多重继承关系:

                      

      在这个继承关系中,父类,子类,都有自己的成员变量。而了类分别覆盖了父类Base1的一个函数f1()和父类Base2的一个函数f2(),并且子类也新增一个函数h()

      源代码如下:

    class Base1
    {
    public:
        Base1(int iBase1) : iBase1(iBase1){}
        void f1(){ cout<<"Base1::f1()"<<endl; }
        void g1(){ cout<<"Base1::g1()"<<endl; }
        void h1(){ cout<<"Base1::h1()"<<endl; }
    private:
        int iBase1;
    };
    
    class Base2
    {
    public:
        Base2(int iBase2) : iBase2(iBase2){}
        void f2(){ cout<<"Base2::f2()"<<endl; }
        void g2(){ cout<<"Base2::g2()"<<endl; }
        void h2(){ cout<<"Base2::h2()"<<endl; }
    private:
        int iBase2;
    };
    
    class Derive : public Base1, public Base2
    {
    public:
        Derive(int iBase1, int iBase2, int iDerive) : Base1(iBase1), Base2(iBase2), iDerive(iDerive) {}
        void f1(){ cout<<"Derive::f1()"<<endl; }
        void g2(){ cout<<"Derive::g2()"<<endl; }
        void h() { cout<<"Derive::h()"<<endl; }
    private:
        int iDerive;
    };

      我们通过下面的程序来查看子类实例的内存布局:下面程序中,我使用了一个指针变量p指向子类的地址。

    Derive d(10, 20, 100);
    int *p = (int *)&d;
    cout<<endl;
    cout << "     Base1::" << endl;
    cout << "          iBase1 = "<<*p++<<endl;;
    cout << "     Base2::" << endl;
    cout << "          iBase2 = "<<*p++<<endl;
    cout << "     Derive::" << endl;
    cout << "          iDerive = "<<*p<<endl;;

      运行结果如下:

                                                  

      父类和子类的内存布局在VC中的体现如下:

    1>class Base1 size(4):
    1> +---
    1> 0 | iBase1
    1> +---
    1>class Base2 size(4):
    1> +---
    1> 0 | iBase2
    1> +---
    1>class Derive size(12):
    1> +---
    1> | +--- (base class Base1)
    1> 0 | | iBase1
    1> | +---
    1> | +--- (base class Base2)
    1> 4 | | iBase2
    1> | +---
    1> 8 | iDerive

      可以看到:

        1、在没有虚函数的情况下,多重继承与单继承没有多大区别, 父类和父类的成员变量根据其继承和声明顺序依次存放后面,子类的成员变量放在最末尾。

    有虚函数的单继承

      下面,我们假设有如下所示的单继承关系:

                                        

      在这个继承关系中,父类,子类,都有自己的成员变量。父类有3个虚函数,分别是f(),g(),h()。子类重写了父类的f()和g()函数,子类同时新增了虚函数h1()

      源代码如下:

    class Base
    {
    public:
        Base(int iBase) : iBase(iBase){}
        virtual void f(){ cout<<"Base::f()"<<endl; }
        virtual void g(){ cout<<"Base::g()"<<endl; }
        virtual void h(){ cout<<"Base::h()"<<endl; }
    private:
        int iBase;
    };
    
    class Derive : public Base
    {
    public:
        Derive(int iBase, int iDerive) : Base(iBase), iDerive(iDerive) {}
        virtual void f() { cout<<"Derive::f()"<<endl; }
        virtual void g() { cout<<"Derive::g()"<<endl; }
        virtual void h1(){ cout<<"Derive::h1()"<<endl; }
    private:
        int iDerive;
    };

      我们通过下面这个小程序来查看子类实例的内存布局:下面程序中,我使用了一个指向指针的指针变量pVtable指向子类的地址,后面的测试程序也这样来进行测试。

    typedef void (*Func)();
    Derive d(10, 100);
    int **pVtable = (int **)&d;
    Func pFun = NULL;
    
    cout << "  [0]   Base::vftable" << endl;
    pFun = (Func)pVtable[0][0];
    cout << "        [0] ";
    pFun();
    
    pFun = (Func)pVtable[0][1];
    cout << "        [1] ";
    pFun();
    
    pFun = (Func)pVtable[0][2];
    cout << "        [2] ";
    pFun();
    
    pFun = (Func)pVtable[0][3];
    cout << "        [3] "; 
    pFun();
    
    pFun = (Func)pVtable[0][4];
    cout << "        [4] "<<pFun<<endl;
    
    cout << "  [1]   Base::iBase = "<<(int)pVtable[1]<<endl;
    cout << "  [2]   Derive::iDerive = "<<(int)pVtable[2]<<endl;

      运行界面如下:

                                                  

      父类和子类在内存中的布局如下:

    1>class Base size(8):
    1> +---
    1> 0 | {vfptr}
    1> 4 | iBase
    1> +---
    1>Base::$vftable@:
    1> | &Base_meta
    1> |  0
    1> 0 | &Base::f
    1> 1 | &Base::g
    1> 2 | &Base::h
    
    1>class Derive size(12):
    1> +---
    1> | +--- (base class Base)
    1> 0 | | {vfptr}
    1> 4 | | iBase
    1> | +---
    1> 8 | iDerive
    1> +---
    1>Derive::$vftable@:
    1> | &Derive_meta
    1> |  0
    1> 0 | &Derive::f
    1> 1 | &Derive::g
    1> 2 | &Base::h
    1> 3 | &Derive::h1

      可以看出:

        1、虚函数表在类的最开始位置

        2、子类覆盖父类的虚函数在虚函数表得到体现

        3、子类新增的虚函数添加到虚函数表的末尾

     

    有虚函数的多继承

      下面,我们假设有如下所示的多重继承关系:

                                 

      在这个继承关系中,父类,子类,都有自己的成员变量。而子类多重继承父类Base1和Base2以及Base3,都分别重写他们的一个虚函数,并且新增了两个虚函数derive_f1()和derive_g1()

      源代码:

    class Base1
    {
    public:
        Base1(int iBase1) : iBase1(iBase1){}
        virtual void f1(){ cout<<"Base1::f1()"<<endl; }
        virtual void g1(){ cout<<"Base1::g1()"<<endl; }
        virtual void h1(){ cout<<"Base1::h1()"<<endl; }
    private:
        int iBase1;
    };
    
    class Base2
    {
    public:
        Base2(int iBase2) : iBase2(iBase2){}
        virtual void f2(){ cout<<"Base2::f2()"<<endl; }
        virtual void g2(){ cout<<"Base2::g2()"<<endl; }
        virtual void h2(){ cout<<"Base2::h2()"<<endl; }
    private:
        int iBase2;
    };
    
    class Base3
    {
    public:
        Base3(int iBase3) : iBase3(iBase3){}
        virtual void f3(){ cout<<"Base3::f3()"<<endl; }
        virtual void g3(){ cout<<"Base3::g3()"<<endl; }
        virtual void h3(){ cout<<"Base3::h3()"<<endl; }
    private:
        int iBase3;
    };
    
    class Derive : public Base1, public Base2, public Base3
    {
    public:
        Derive(int iBase1, int iBase2, int iBase3, int iDerive) : Base1(iBase1), Base2(iBase2), Base3(iBase3), iDerive(iDerive) {}
        virtual void f1() { cout<<"Derive::f()"<<endl; }
        virtual void g2() { cout<<"Derive::g()"<<endl; }
        virtual void h3() { cout<<"Derive::h1()"<<endl; }
        virtual void derive_f1(){ cout<<"Derive::derive_f1()"<<endl; }
        virtual void derive_g1(){ cout<<"Derive::derive_g1()"<<endl; }
    private:
        int iDerive;
    };

      我们用下面代码来测试子类Derive的布局情况:

    typedef void (*Func)();
    Derive d(10, 20 ,30, 100);
    int **pVtable = (int **)&d;
    Func pFun = NULL;
    
    cout << "  [0]   Base1::vftable" << endl;
    pFun = (Func)pVtable[0][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[0][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[0][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[0][3];
    cout << "            [3] "; 
    pFun();
    
    pFun = (Func)pVtable[0][4];
    cout << "            [4] ";
    pFun();
    
    pFun = (Func)pVtable[0][5];
    cout << "            [5] "<<pFun<<endl;
    
    cout << "  [1]   Base1::iBase1 = " <<(int)pVtable[1]<<endl;
    
    int pos = sizeof(Base1)/4;
    cout << "  ["<<pos<<"]   Base2::vftable" << endl;
    pFun = (Func)pVtable[pos][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[pos][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[pos][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[0][3];
    cout << "            [3] "<<pFun<<endl;
    
    cout << "  ["<<pos + 1<<"]   Base2::iBase2 = " <<(int)pVtable[pos + 1]<<endl;
    
    pos += sizeof(Base2)/4;
    cout << "  ["<<pos<<"]   Base3::vftable" << endl;
    pFun = (Func)pVtable[pos][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[pos][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[pos][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[0][3];
    cout << "            [3] "<<pFun<<endl;
    
    pos++;
    cout << "  ["<<pos<<"]   Base3::iBase3 = " <<(int)pVtable[pos]<<endl;
    
    pos++;
    cout << "  ["<<pos<<"]   Derive::iDerive = " <<(int)pVtable[pos]<<endl;

      运行界面如下:

                                          

      VC输出窗口输出的子类内存的布局信息如下:

    1>class Derive size(28):
    1> +---
    1> | +--- (base class Base1)
    1> 0 | | {vfptr}
    1> 4 | | iBase1
    1> | +---
    1> | +--- (base class Base2)
    1> 8 | | {vfptr}
    1>12 | | iBase2
    1> | +---
    1> | +--- (base class Base3)
    1>16 | | {vfptr}
    1>20 | | iBase3
    1> | +---
    1>24 | iDerive
    1> +---
    1>Derive::$vftable@Base1@:
    1> | &Derive_meta
    1> |  0
    1> 0 | &Derive::f1
    1> 1 | &Base1::g1
    1> 2 | &Base1::h1
    1> 3 | &Derive::derive_f1
    1> 4 | &Derive::derive_g1
    1>Derive::$vftable@Base2@:
    1> | -8
    1> 0 | &Base2::f2
    1> 1 | &Derive::g2
    1> 2 | &Base2::h2
    1>Derive::$vftable@Base3@:
    1> | -16
    1> 0 | &Base3::f3
    1> 1 | &Base3::g3
    1> 2 | &Derive::h3

      可以看出:

        1、在子类中的每一个父类都有一个虚函数表

        2、子类新增的虚函数,按继承顺序放在第一个父类的虚函数表中

        3、子类覆盖父类的虚函数在每一个父类的虚函数表得到体现

    有虚函数的重复多继承

      下面,我们假设有如下所示的多重继承关系:

                                       

      孩子类GrandDerive多重继承于Derive1和Derive2。子类Derive1和Derive2继承同一个父类Base。

      源代码如下:

    class Base
    {
    public:
        Base(int iBase) : iBase(iBase){}
        virtual void f(){ cout<<"Base::f()"<<endl; }
        virtual void g(){ cout<<"Base::g()"<<endl; }
        virtual void h(){ cout<<"Base::h()"<<endl; }
    private:
        int iBase;
    };
    
    class Derive1 : public Base
    {
    public:
        Derive1(int iBase, int iDerive1) : Base(iBase), iDerive1(iDerive1) {}
        virtual void f() { cout<<"Derive1::f()"<<endl; }
        virtual void g() { cout<<"Derive1::g()"<<endl; }
        virtual void h1(){ cout<<"Derive1::h1()"<<endl; }
        virtual void derive1_f1(){ cout<<"Derive1::derive1_f1()"<<endl; }
        virtual void derive1_g1(){ cout<<"Derive1::derive1_g1()"<<endl; }
    private:
        int iDerive1;
    };
    
    class Derive2 : public Base
    {
    public:
        Derive2(int iBase, int iDerive2) : Base(iBase), iDerive2(iDerive2) {}
        virtual void f() { cout<<"Derive2::f()"<<endl; }
        virtual void g() { cout<<"Derive2::g()"<<endl; }
        virtual void h2(){ cout<<"Derive2::h2()"<<endl; }
        virtual void derive2_f2(){ cout<<"Derive2::derive2_f2()"<<endl; }
        virtual void derive2_g2(){ cout<<"Derive2::derive2_g2()"<<endl; }
    private:
        int iDerive2;
    };
    
    class GrandDerive : public Derive1, public Derive2
    {
    public:
        GrandDerive(int iBase, int iDerive1, int iDerive2, int iGrandDerive) : Derive1(iBase, iDerive1), Derive2(iBase, iDerive2), iGrandDerive(iGrandDerive) {}
        virtual void f() { cout<<"GrandDerive::f()"<<endl; }
        virtual void g() { cout<<"GrandDerive::g()"<<endl; }
        virtual void h() { cout<<"GrandDerive::h()"<<endl; }
        virtual void derive1_f1(){ cout<<"GrandDerive::derive1_f1()"<<endl; }
        virtual void derive2_g2(){ cout<<"GrandDerive::derive2_g2()"<<endl; }
        virtual void grandderive_f(){ cout<<"GrandDerive::grandderive_f()"<<endl; }
        virtual void grandderive_g(){ cout<<"GrandDerive::grandderive_g()"<<endl; }
    private:
        int iGrandDerive;
    };

      下面代码来测试孩子类GrandDerive的布局情况:

    typedef void (*Func)();
    GrandDerive gd(10, 100 ,200, 1000);
    int **pVtable = (int **)&gd;
    Func pFun = NULL;
    
    cout << "  [0]   Derive1::vftable" << endl;
    pFun = (Func)pVtable[0][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[0][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[0][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[0][3];
    cout << "            [3] "; 
    pFun();
    
    pFun = (Func)pVtable[0][4];
    cout << "            [4] ";
    pFun();
    
    pFun = (Func)pVtable[0][5];
    cout << "            [5] ";
    pFun();
    
    pFun = (Func)pVtable[0][6];
    cout << "            [6] ";
    pFun();
    
    pFun = (Func)pVtable[0][7];
    cout << "            [7] ";
    pFun();
    
    pFun = (Func)pVtable[0][8];
    cout << "            [8] "<<pFun<<endl;
    
    cout << "  [1]   Base::iBase = " <<(int)pVtable[1]<<endl;
    
    cout << "  [2]   Derive1::iDerive1 = " <<(int)pVtable[2]<<endl;
    
    int pos = sizeof(Derive1)/4;
    cout << "  ["<<pos<<"]   Derive2::vftable" << endl;
    pFun = (Func)pVtable[pos][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[pos][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[pos][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[pos][3];
    cout << "            [3] "; 
    pFun();
    
    pFun = (Func)pVtable[pos][4];
    cout << "            [4] ";
    pFun();
    
    pFun = (Func)pVtable[pos][5];
    cout << "            [5] ";
    pFun();
    
    pFun = (Func)pVtable[pos][6];
    cout << "            [6] "<<pFun<<endl;
    
    pos++;
    cout << "  ["<<pos<<"]   Base::iBase = " <<(int)pVtable[pos]<<endl;
    
    pos++;
    cout << "  ["<<pos<<"]   Derive2::iDerive2 = " <<(int)pVtable[pos]<<endl;
    
    pos++;
    cout << "  ["<<pos<<"]   GrandDerive::iGrandDerive = " <<(int)pVtable[pos]<<endl;

      运行界面如下:

                                         

      孩子类的内存布局如下:

    1>class GrandDerive size(28):
    1> +---
    1> | +--- (base class Derive1)
    1> | | +--- (base class Base)
    1> 0 | | | {vfptr}
    1> 4 | | | iBase
    1> | | +---
    1> 8 | | iDerive1
    1> | +---
    1> | +--- (base class Derive2)
    1> | | +--- (base class Base)
    1>12 | | | {vfptr}
    1>16 | | | iBase
    1> | | +---
    1>20 | | iDerive2
    1> | +---
    1>24 | iGrandDerive
    1> +---
    1>GrandDerive::$vftable@Derive1@:
    1> | &GrandDerive_meta
    1> |  0
    1> 0 | &GrandDerive::f
    1> 1 | &GrandDerive::g
    1> 2 | &GrandDerive::h
    1> 3 | &Derive1::h1
    1> 4 | &GrandDerive::derive1_f1
    1> 5 | &Derive1::derive1_g1
    1> 6 | &GrandDerive::grandderive_f
    1> 7 | &GrandDerive::grandderive_g
    1>GrandDerive::$vftable@Derive2@:
    1> | -12
    1> 0 | &thunk: this-=12; goto GrandDerive::f
    1> 1 | &thunk: this-=12; goto GrandDerive::g
    1> 2 | &thunk: this-=12; goto GrandDerive::h
    1> 3 | &Derive2::h2
    1> 4 | &Derive2::derive2_f2
    1> 5 | &GrandDerive::derive2_g2

      可以看出:

        1、孩子类存在父类的两份拷贝。

        2、孩子类的第二个父类的虚函数表,有些虚函数条目直接跳转到第一个父类的虚函数表的同名函数的,这只是VC的实现方式而已,其他编译器可能不是这样。

    有虚函数的虚拟继承

      下面,我们假设有如下所示的多重继承关系:

                                    

      这个类图与"有虚函数的重复多继承"一致,主要区别在于Derive1和Derive2虚拟继承于Base。

      源代码如下:

    class Base
    {
    public:
        Base(int iBase) : iBase(iBase){}
        virtual void f(){ cout<<"Base::f()"<<endl; }
        virtual void g(){ cout<<"Base::g()"<<endl; }
        virtual void h(){ cout<<"Base::h()"<<endl; }
    private:
        int iBase;
    };
    
    class Derive1 : virtual public Base
    {
    public:
        Derive1(int iBase, int iDerive1) : Base(iBase), iDerive1(iDerive1) {}
        virtual void f() { cout<<"Derive1::f()"<<endl; }
        virtual void g() { cout<<"Derive1::g()"<<endl; }
        virtual void h1(){ cout<<"Derive1::h1()"<<endl; }
        virtual void derive1_f1(){ cout<<"Derive1::derive1_f1()"<<endl; }
        virtual void derive1_g1(){ cout<<"Derive1::derive1_g1()"<<endl; }
    private:
        int iDerive1;
    };
    
    class Derive2 :virtual public Base
    {
    public:
        Derive2(int iBase, int iDerive2) : Base(iBase), iDerive2(iDerive2) {}
        virtual void f() { cout<<"Derive2::f()"<<endl; }
        virtual void g() { cout<<"Derive2::g()"<<endl; }
        virtual void h2(){ cout<<"Derive2::h2()"<<endl; }
        virtual void derive2_f2(){ cout<<"Derive2::derive2_f2()"<<endl; }
        virtual void derive2_g2(){ cout<<"Derive2::derive2_g2()"<<endl; }
    private:
        int iDerive2;
    };
    
    class GrandDerive : public Derive1, public Derive2
    {
    public:
        GrandDerive(int iBase, int iDerive1, int iDerive2, int iGrandDerive) : Base(iBase), Derive1(iBase, iDerive1), Derive2(iBase, iDerive2)                                                                          , iGrandDerive(iGrandDerive) {}
        virtual void f() { cout<<"GrandDerive::f()"<<endl; }
        virtual void g() { cout<<"GrandDerive::g()"<<endl; }
        virtual void h() { cout<<"GrandDerive::h()"<<endl; }
        virtual void derive1_f1(){ cout<<"GrandDerive::derive1_f1()"<<endl; }
        virtual void derive2_g2(){ cout<<"GrandDerive::derive2_g2()"<<endl; }
        virtual void grandderive_f(){ cout<<"GrandDerive::grandderive_f()"<<endl; }
        virtual void grandderive_g(){ cout<<"GrandDerive::grandderive_g()"<<endl; }
    private:
        int iGrandDerive;
    };

      孩子类GrandDerive直接调用虚基类Base的构造函数,子类Derive1和Derive2在初始化自身的时候不再调用基类Base的构造函数。

      我们使用下面程序代码来测试孩子类的布局情况:

    typedef void (*Func)();
    GrandDerive gd(10, 100 ,200, 1000);
    int **pVtable = (int **)&gd;
    Func pFun = NULL;
    
    cout << "  [0]   Derive1::vftable" << endl;
    pFun = (Func)pVtable[0][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[0][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[0][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[0][3];
    cout << "            [3] "; 
    pFun();
    
    pFun = (Func)pVtable[0][4];
    cout << "            [4] ";
    pFun();
    
    pFun = (Func)pVtable[0][5];
    cout << "            [5] "<<pFun<<endl;
    
    cout << "  [1]   Derive1::vbase" <<*(pVtable[1])<<endl;
    
    cout << "  [2]   Derive1::iDerive1 = " <<(int)pVtable[2]<<endl;
    
    cout << "  [3]   Derive2::vftable" << endl;
    pFun = (Func)pVtable[3][0];
    cout << "            [0] ";
    pFun();
    
    pFun = (Func)pVtable[3][1];
    cout << "            [1] ";
    pFun();
    
    pFun = (Func)pVtable[3][2];
    cout << "            [2] ";
    pFun();
    
    pFun = (Func)pVtable[3][3];
    cout << "            [3] "<<pFun<<endl;
    
    cout << "  [4]   Derive2::vbase" <<*(pVtable[4])<<endl;
    
    cout << "  [5]   Derive2::iDerive1 = " <<(int)pVtable[5]<<endl;
    
    cout << "  [6]   GrandDerive::iGrandDerive = " <<(int)pVtable[6]<<endl;
    
    cout << "  [7]                               " <<(int)pVtable[7];  //这个不懂是什么 
    cout << "  [8]   Base::vftable" << endl;
    pFun = (Func)pVtable[8][0];   
    cout << "            [0] "<<endl;  //这里必须加上换行符,不然运行崩溃,不懂原因   
    pFun();
    
    pFun = (Func)pVtable[8][1];      
    cout << "            [1] "<<endl;  //这里必须加上换行符,不然运行崩溃,不懂原因   
    pFun();
    
    pFun = (Func)pVtable[8][2];
    cout << "            [2] "<<endl;  //这里必须加上换行符,不然运行崩溃,不懂原因   
    pFun();
    
    pFun = (Func)pVtable[8][3];
    cout << "            [3] "<<pFun<<endl;

    cout << "  [9]   Base::iBase = " <<(int)pVtable[9]<<endl;

      运行界面如下:     

                                        

      在上面的运行界面,我有两点疑问,已经用黄色标记,同时在上面的测试代码中我也用注释标明。如谁知道原因,麻烦告知,非常感谢。

      下面是VC输出的GrandDerive的布局信息。

    1>  class GrandDerive    size(40):
    1>      +---
    1>      | +--- (base class Derive1)
    1>   0    | | {vfptr}
    1>   4    | | {vbptr}
    1>   8    | | iDerive1
    1>      | +---
    1>      | +--- (base class Derive2)
    1>  12    | | {vfptr}
    1>  16    | | {vbptr}
    1>  20    | | iDerive2
    1>      | +---
    1>  24    | iGrandDerive
    1>      +---
    1>  28    | (vtordisp for vbase Base)  //上面就是这输出0,
    1>      +--- (virtual base Base)
    1>  32    | {vfptr}
    1>  36    | iBase
    1>      +---
    1>  
    1>  GrandDerive::$vftable@Derive1@:
    1>      | &GrandDerive_meta
    1>      |  0
    1>   0    | &Derive1::h1
    1>   1    | &GrandDerive::derive1_f1
    1>   2    | &Derive1::derive1_g1
    1>   3    | &GrandDerive::grandderive_f
    1>   4    | &GrandDerive::grandderive_g
    1>  
    1>  GrandDerive::$vftable@Derive2@:
    1>      | -12
    1>   0    | &Derive2::h2
    1>   1    | &Derive2::derive2_f2
    1>   2    | &GrandDerive::derive2_g2
    1>  
    1>  GrandDerive::$vbtable@Derive1@:
    1>   0    | -4
    1>   1    | 28 (GrandDerived(Derive1+4)Base)
    1>  
    1>  GrandDerive::$vbtable@Derive2@:
    1>   0    | -4
    1>   1    | 16 (GrandDerived(Derive2+4)Base)
    1>  
    1>  GrandDerive::$vftable@Base@:
    1>      | -32
    1>   0    | &(vtordisp) GrandDerive::f
    1>   1    | &(vtordisp) GrandDerive::g
    1>   2    | &(vtordisp) GrandDerive::h

      可以看出:

        1、虚基类放在孩子类的末尾

        2、VC实现虚基类的方式是每一个子类用一个指向虚基类表的指针vbptr,同时,子类的虚函数表不在包含父类的虚函数地址。

      

      各个编译器实现的内存布局方式可能各不相同,在这里特指VS2010,如果在其他编译器运行上述代码,可能结果会不一样。

      完!

  • 相关阅读:
    Web前端之jQuery 的10大操作技巧
    Python开发者须知 —— Bottle框架常见的几个坑
    string、const char*、 char* 、char[]相互转换
    SLAM中的变换(旋转与位移)表示方法
    SLAM
    二叉搜索树(BST)
    Linux下OSG的编译和安装以及遇到的问题
    CMake--Set用法
    CMake--List用法
    php面向对象面试题
  • 原文地址:https://www.cnblogs.com/venow/p/2674300.html
Copyright © 2020-2023  润新知