virtual inheritance memory layout

Reprinted from: http://blog.csdn.net/xsh_123321/article/details/5956289

1. Why virtual inheritance is needed

As shown in the figure below, if Der::Fun or Der::m_nValue is accessed, it will bring ambiguity, and it is impossible to determine whether Base1 or Base2 is called. Therefore, in order to solve the ambiguity of member access in the case of multiple inheritance, virtual inheritance mechanism.

 

General Inheritance Virtual Inheritance

                    

 

2. Virtual inheritance implementation

Under virtual inheritance, Der avoids ambiguity by sharing the virtual base class SuperBase. The virtual base class pointers are stored in Base1 and Base2 respectively. Der inherits Base1 and Base2, including the virtual base class pointers of Base1 and Base2, and points to the same block. The memory area, so that Der can indirectly access the members of the virtual base class, as shown in the following figure:

3. Different compiler implementations

 

Different compilers have different methods for indirect access. The following takes GCC and VC as examples, and the following codes are used for experiments:

 

class SuperBase

{

public:

    int m_nValue;

    void Fun(){cout<<"SuperBase1"<<endl;}

    virtual ~SuperBase(){}

};

 

class Base1:  virtual public SuperBase

{

public:

virtual ~ Base1(){}

};

class Base2:  virtual public SuperBase

{

 

public:

virtual ~ Base2(){}

 

};

 

class Der:public Base1, public Base2

{

 

public:

virtual ~ Der(){}

 

};

 

void main()

{

cout<<sizeof(SuperBase)<<sizeof(Base1)<<sizeof(Base2)<<sizeof(Der)<<endl;

}

 

1) The result in GCC is 8, 12, 12, 16

Analysis: sizeof(SuperBase) = sizeof(int) + virtual function table pointer

sizeof(Base1) = sizeof(Base2) = sizeof(int) + virtual function pointer + virtual base class pointer

sizeof(Der) = sizeof(int) + virtual base class pointer in Base1 + Base2 virtual base class pointer + virtual function pointer 

GCC shares the virtual function table pointer, that is to say, if the parent class already has a virtual function table pointer, then the subclass shares the virtual function table pointer space of the parent class and does not occupy additional space. This is different from VC, which is in virtual inheritance. In this case, the parent class virtual function table pointer is not shared, as detailed below.

 

2) The results in VC are: 8, 16, 16, 24

 

Analysis: sizeof(SuperBase) = sizeof(int) + virtual function table pointer

sizeof(Base1) = sizeof(Base2) = sizeof(int) + SuperBase virtual function pointer + virtual base class pointer + own virtual function pointer

sizeof(Der) = sizeof(int) + virtual base class pointer in Base1 + virtual base class pointer in Base2 + Base1 virtual function pointer + Base2 virtual function pointer + own virtual function pointer

 

 

If the virtual inheritance is removed, the result will be the same as the GCC result, A, B, C are all 8, D is 16, the reason is that the VC compiler for non-virtual inheritance, the parent class and the subclass share the virtual function table pointer .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809262&siteId=291194637
Recommended