Deep understanding of virtual inheritance in C++

  • 2020-04-02 01:00:06
  • OfStack

Today I took a look at virtual inheritance, which I haven't used much before. The details are as follows:
The parent class:  

class   CParent 
{ 
.... 
}; 
 The declaration of inherited classes is special:  
class   CChild   :   virtual   public   CParent 
{ 
.... 
}  

Excuse me, what is the function and meaning of this "virtual"?
---------------------------------------------------------------
Representing virtual inheritance and normal inheritance are two ways of inheritance in C++.
For example, B1, B2     Inherited A     And C inherits B1 and B2 multiple times
If common inheritance occurs, then C contains two copies of A, one from B1 and one from B2
Virtual inheritance contains only one copy of A
---------------------------------------------------------------
What does this "virtual" do and what does it mean?
Prove that this CParent is CChild     The virtual base class
Virtual base class     the     role
Virtual base class is: class     ttf_subclass     :     virtual     public     BaseClass     Base class declared in virtual!! Because c + + supports multiple inheritance, so for a derived class has several direct parent class, and there are several possible in a few direct parent respectively inherited from a base class (that is, the father of the parent class), so the structure resulting derived classes, there will be a final derived class contains many of the same base class, and then by the appearance of ambiguity problem (don't know which one to call the base class member variables and functions), in order to solve this problem, you need to use the virtual base class, is the base class only generates a memory area, eventually in the derived class will only contain a base class
The typical case where virtual base classes are required is as follows:
                                              a.
                                          /     \
                                      B             C
                                          \     /
                                              D
Where, D inherits from BC, and BC inherits from A respectively, so A should be virtually inherited by BC respectively
Program............  

class   A   { 
    public: 
        void   printA()   {cout<<"this   is   An";} 
}; 
class   B:virtual   public   A; 
class   C:virtual   public   A; 
class   D:public   B,public   C;  

This way, after D is constructed, there is only one A in its storage area, so there is no ambiguity problem
For example: D     D = new     D;
At this time, if use d.php (); No problem; But if B and C do not inherit from A, there is ambiguity
Multiple inheritance in COM does not use virtual inheritance, which results in VTBL that is not compatible with COM. If IX and IY inherit IUnknown by virtual inheritance, the first three functions in IX and IY's VTBL will not point to the three member functions of IUnknown (see Com technology insider).
IUnknown               - >                       IX                                              
                                                                                                      --- >               The CA
IUnknown               - >                       IY
Generally, converting a pointer of one type to another does not change the value of the pointer, but in order to support multiple inheritance, C++ must change the value of the class pointer in some cases. This is the case with multiinterface inheritance in COM
CA* pA = new CA();
IY * PC = pA;                     It will be changed by the compiler     IY * pC = (char *)pA + deltaIY(an offset);
The destructor of the base class should also be defined so that the virtual is invoked when an instance of the inherited class is destructed to clear the resources used by the base class.

Related articles: