The concept of virtual inheritance is mainly to solve the problem of multiple inheritance of C + +, for the simplest example:
Copy Code code 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 ();
}
The Liger.op () on the above will cause an error, prompting the ambiguous member variable, because both Tiger and Lion contain the OP () action of the parent class animal.
In this case, the layout of the Oneliger objects in memory from low to High is the following:
1, Animal member variable
2, inherit Tiger's member variable
Including OP ()
3, inherit the Lion member variable
//also includes OP ()
4, the member variable of Liger itself
PS: The layout of an object in memory is first, if there is a virtual function is a virtual table, a virtual table is a pointer to an array of function pointers, then a member variable, and, if it is a normal inheritance, is first the member variable of the root parent class, then the secondary parent class member variable, and then finally the member variable of its own. [Virtual inheritance is the opposite], a member function is compiled into a global function that is not stored in an object space, when a member function needs to be called, the corresponding function is found by the class name, and then the this pointer to the object is passed to the function:
For example, this code
CTest test;
Test.print ();
The compiler will convert internally to: (pseudo code)
CTest test;
Ctest_print (&test); The CTest print function is converted to: Ctest_print (ctest* const this);
So it's not much different from a normal function call.
The actual function should be to find the object, that is, according to the This pointer
In order to solve the problem of multiple inheritance above, the concept of virtual inheritance is proposed in C + +. Virtual inheritance is to keep only a copy of the parent class in the subclass, and to take the class above, "If you have a copy of the parent class, use a copy of the parent class, and if not, add a copy":
Copy Code code 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 ();
}
The layout of the Liger object in memory now becomes:
4, animal member variable
3, inherit Tiger's member variable
Including OP ()
2, inherit the Lion member variable
Already contains a copy, so OP () is not included
1, the member variable of Liger itself
So there is only one copy of the animal object in memory, so there is no fuzzy problem;