The concept __c++ of C + + virtual inheritance

Source: Internet
Author: User

http://blog.csdn.net/wangxingbao4227/article/details/6772579

The concept of virtual inheritance in C + +

To resolve data inconsistencies caused by data members with the same name inherited from different sources in memory, the common base class is set to the virtual base class. At this point, the data members of the same name inherited from different paths have only one copy in memory, and the same function name has only one mapping. This not only solves the problem of two semantics, but also saves the memory and avoids the problem of inconsistent data.
Class derived class Name: Virtual Inheritance method base class name
Virtual is a keyword that declares the base class to be a virtual base class for a derived class.
In multiple inheritance situations, the virtual base class keyword has the same scope and inheritance method keywords, and only works on the base class immediately following it.
After a virtual base class is declared, the virtual base class always maintains a copy of the same base class child, along with the derived class, during further derivation.

C + + virtual inheritance

◇ Concept:

C + + uses dummy inheritance (virtual inheritance) to resolve data inconsistencies caused by data members of the same name inherited from different sources in memory, setting the common base class to virtual base classes. At this point, the data members of the same name inherited from different paths have only one copy in memory, and the same function name has only one mapping.

◇ Problem Solving:

The problem of two semantics is solved, the memory is saved and the data inconsistency is avoided.
◇ Synonyms:
Virtual base class (take a verb as a noun)
When there is a common base class on more than one inheritance path, at the confluence of some of these paths, the common base class produces multiple instances (or multiple replicas), and if you want to save only one instance of the base class, you can describe the common base class as a virtual base class.

◇ Grammar:

Class derived class: Virtual base class 1,virtual base class 2,...,virtual base class N

{

...//derived class member declaration

};

◇ Order of execution

The constructor of the virtual base class is executed first, and the constructors of multiple virtual base classes are constructed in the order of inheritance;

Executes the constructor of a base class, and the constructors of multiple base classes are constructed in the order in which they are inherited;

The constructor of the member object is executed, and the constructors of multiple member objects are constructed in the order of the declarations;

Executes the derived class's own constructor;

The destructor is executed in the order opposite to the construction;

Mark

A call to a virtual base class constructor is listed in the member initialization list of a constructor in a derived class that derives directly or indirectly from the virtual base class. However, only the constructor of the most derived class that is used to establish the object calls the constructor of the virtual base class, and the call to the constructor of the virtual base class that is listed in all base classes of the derived class is ignored in execution, guaranteeing that the virtual base class child object is initialized only once.

When a call to a virtual base class and a non-virtual base class constructor occurs simultaneously in a member initialization list, the constructor of the virtual base class is performed before the constructor of the Non-virtual base class.

◇ Causal:

Multiple inheritance-> ambiguity-> virtual inheritance Solution

◇ Ambiguity:

1://-----------------------------------------------------

7: #include "stdafx.h"
8: #include <iostream>
9:using namespace Std;
10:
12:class Base
13: {
14:public:
15:base () {cout << "Base called ..." << Endl;}
16:void print () {cout << "Base print ..." <<endl}
17:private:
18:};
19:
22: {
23:public:
24:sub () {cout << "Sub called ..." << Endl;}
25:void print () {cout << "Sub print ..." << Endl}
26:private:
27:};
28:
31: {
32:public:
33:child () {cout << "child called ..." << Endl;}
34:private:
35:};
36:
37:int Main (int argc, char* argv[])
38: {
39:child C;
40:
://c.print ();  
43:
45:c.base::p rint ();
46:c.sub::p rint ();
47:
48:system ("pause");
49:return 0;
50:}

◇ Multiple Inheritance:

1://-----------------------------------------------------  
2://Name: Blog_virtual_inherit.cpp  
 
3://Description: C + + Virtual Inheritance Learning Demo  
4://Environment: VS2005  
5://blog:pppboy.blog.163.com  
6://----------------------------------------------------  
7: #include "stdafx.h"  
8: #include <iostream>
9:using namespace Std;  
10:
11:int gflag = 0;  
12:
13:class Base  
14: {
15:public:  
16:base () {cout << "Base called:" << gflag++ << Endl;}  
17:void print () {cout << "Base print" <<ENDL;}  
18:};
19:
20:class Mid1:public Base  
21: {
22:public:  
23:mid1 () {cout << "Mid1 called" << Endl;}  
24:private:  
25:};
26:
27:class Mid2:public Base  
28: {
29:public:  
30:mid2 () {cout << "Mid2 called" << Endl;}  
31:};
32:
33:class child:public Mid1, public Mid2  
34: {
35:public:  
36:child () {cout << "child called" << Endl;}  
37:};
38:
39:int Main (int argc, char* argv[])  
40: {
41:child D;
42:
43://Can not be used in this way, will produce two of Italian  
     D.print ();  
45:
46://can only be used in this way  
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.