C ++ basic syntax: Virtual inheritance

Source: Internet
Author: User

The concept of virtual inheritance is proposed mainly to solve the problem of C ++ multi-inheritance. The simplest example is as follows:
Copy codeThe Code is 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 above liger. op (); will report an error and will prompt the fuzzy member variable, because both tiger and lion contain the op () Operation of the parent class animal.
In this case, the oneliger object layout in the memory is as follows:
1. animal member variables

2. inherit tiger member variables
// Including op ()

3. inherit lion member variables
// Also includes op ()

4. liger member variables

PS:The layout of objects in the memory is first a virtual table if there is a virtual function, and a virtual table is a pointer to a function pointer array, and then a member variable, for normal inheritance, it is first the member variable of the root parent class, then the secondary parent class member variable, and then the final member variable of itself [opposite to virtual inheritance]. when a member function is compiled into a global function, it is not stored in the object space. When you need to call a member function, find the corresponding function using the class name, and then pass the this pointer of the object to the function:

Such code
CTest test;
Test. print ();

The internal part of the compiler will be converted 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 normal function calls.
Actually, the function should find the object, that is, according to the this pointer

To solve the above multi-Inheritance Problem, the concept of virtual inheritance is put forward in c ++. The virtual inheritance is to keep only one copy of the parent class in the subclass, take the above class as an example. "If there is a copy of the parent class, the copy of the parent class will be used. If not, a copy will be added ":
Copy codeThe Code is 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 the memory becomes:
4. animal member variables

3. inherit tiger member variables
// Including op ()

2. inherit lion member variables
// A copy is included, so op () is not included ()

1. liger member variables

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.