C++ basic syntax: virtual inheritance

Time:2022-6-18

The concept of virtual inheritance is proposed to solve the problem of multiple inheritance in c++. Take the simplest example:

Copy codeThe codes are as follows:
class animal{
        public :
              void op()
                  {cout << “hello animal” ;}
 };
class tiger : public animal {
        public :
              void tg()
                  {cout << “this is  tiger” ;}
};
class lion : public animal {
        public :
              void lo()
                  {cout << “this is lion” ;}
};
class liger : public tiger, public lion {
        public :
              void lo()
                  {cout << “this is lion” ;}
};
int main()
{
     class liger  oneliger ;
     liger.op() ; 
}

Liger op() ; An error will be reported, and fuzzy member variables will be prompted, because tiger and lion contain op() operations of the parent class animal.
At this time, the layout of oneliger objects in memory from low to high is as follows:
1. Member variables of animal

2. Inherit member variables of tiger
/ / include op ()

3. Inherit member variables from Lion
/ / also includes op ()

4. Member variables of liger itself

PS:The layout of objects in memory is first of all, if there is a virtual function, it is a virtual table. The virtual table is a pointer to a function pointer array, then a member variable. If it is a common inheritance, it is first the member variable of the most root parent class, then the member variable of the secondary parent class, and then its own member variable [virtual inheritance is the opposite]. The member function is compiled into a global function and is not stored in the object space, When a member function needs to be called, find the corresponding function through the class name, and then pass the this pointer of the object to the function:

Such code
CTest     test; 
test.print(); 

The compiler internally converts to: (pseudo code)
CTest   test; 
CTest_ print(   &test   ); / / the print function of ctest is converted to: ctest_ print(   CTest*   const   this);  

So this is not much different from ordinary function calls
Actually, the function should find the object, that is, according to the this pointer

In order to solve the above problem of multiple inheritance, the concept of virtual inheritance is proposed in c++. Virtual inheritance is to keep only a copy of the parent class in the subclass. For the subclass above, it means “if there is a copy of the parent class, use the copy of the parent class. If there is no copy, add a copy”:

Copy codeThe codes are as follows:
class animal{
        public :
              void op()
                  {cout << “hello animal” ;}
 };
class tiger : public virtual animal {
        public :
              void tg()
                  {cout << “this is  tiger” ;}
};
class lion : public virtual animal {
        public :
              void lo()
                  {cout << “this is lion” ;}
};
class liger : public tiger, public lion {
        public :
              void lo()
                  {cout << “this is lion” ;}
};
int main()
{
     class liger  oneliger ;
     liger.op() ; 
}

At this point, the layout of the liger object in memory becomes:
4. Member variables of animal

3. Inherit member variables of tiger
/ / include op ()

2. Inherit member variables from Lion
/ / it already contains a copy, so it does not include op ()

1. Member variables of liger itself

In this way, there is only one copy of the animal object in the memory, so there will be no ambiguity;