preface : Virtual inheritance is to solve the problem of diamond inheritance in multiple inheritance . for example : class B,C Inherited classes respectively A. class D Multiple inheritance classes B and C When , class A The data in the D Multiple copies in . By declaring the inheritance relationship, add virtual Keyword can implement virtual inheritance .

One , multiple inheritance

Single inheritance —— A derived class can have at most one base class
multiple inheritance —— A derived class can have multiple base classes

class Class name : Inheritance method Base class 1, Inheritance method Base class 2,…. {….};

Derived classes inherit members of multiple base classes at the same time , Better software reuse
There may be a lot of ambiguity , Multiple base classes may contain variables or functions with the same name

The method of solving access ambiguity in multiple inheritance :

Base class name :: Data member name ( Or member function ( Parameter table ))

Specify which base class member you want to access
#include <iostream> using namespace std; class Bed { public: Bed(int weight) :
weight_(weight) { } void Sleep() { cout << "Sleep ..." << endl; } int weight_;
}; class Sofa { public: Sofa(int weight) : weight_(weight) { } void WatchTV() {
cout << "Watch TV ..." << endl; } int weight_; }; class SofaBed : public Bed,
public Sofa { public: SofaBed() : Bed(0), Sofa(0) { FoldIn(); } void FoldOut()
{ cout << "FoldOut ..." << endl; } void FoldIn() { cout << "FoldIn ..." <<
endl; } }; int main(void) { SofaBed sofaBed; //sofaBed.weight_ = 10; error
//sofaBed.weight_ = 20; error sofaBed.Bed::weight_ = 10; sofaBed.Sofa::weight_
= 20; sofaBed.WatchTV(); sofaBed.FoldOut(); sofaBed.Sleep(); return 0; }
Can't write directly  sofaBed.weight_ = 10; because sofaBed Inherited Sofa and Bed
, Actually weigh_ Two copies of , The point is not clear . Only through

sofaBed.Bed::weight_ = 10; visit , But actually a sofaBed There should be only one weight_, This problem can be solved by virtual base class and virtual inheritance .

Two , Virtual inheritance and virtual base class

When a derived class derives from more than one base class , These base classes are derived from the same base class , When accessing members in this common base class , Will produce ambiguity , Virtual base classes can be used to solve this problem .

The introduction of virtual base class

For situations with common base classes


with virtual Modifier base class example :class B1:virtual public BB


It is mainly used to solve the ambiguity problem caused by multiple inheritance to the same base class .

Provide unique base class members for farthest derived classes , Without duplicate copies

       class CBase { };

  class ChildA1:virtual public CBase{ };

  class ChildA2:virtual public CBase{ };

  class ChildB:public ChildA1,ChildA2{ };

   In class ChildB Object of , Class only CBase Object data for

The characteristics of virtual base class :

        (1) The parameters of the virtual base class constructor must be initialized by the newly derived class ( Even if not directly inherited ).       

   (2) The constructor of a virtual base class executes before that of a non virtual base class .

Let's see the output of a program :
#include "stdafx.h" #include<iostream> using namespace std; class CBase{
protected: int a; public: CBase(int na) { a = na; cout << "CBase constructor!"
<< endl; } ~CBase() { cout << "CBase deconstructor!" << endl; } }; class
ChildA1: virtual public CBase { public: ChildA1(int na):CBase(na) { cout <<
"ChildA1 constructor!" << endl; } ~ChildA1() { cout << "ChildA1 deconstructor!"
<< endl; } int GetA() { return a; } }; class ChildA2 : virtual public CBase {
public: ChildA2(int na):CBase(na) { cout<< " ChildA2 constructor!" << endl; }
~ChildA2() { cout<< "ChildA2 deconstructor!" << endl; } int GetA() { return a;
} }; class ChildB:public ChildA1,public ChildA2 { public: ChildB(int a1,int
a2,int a3):ChildA1(a1),ChildA2(a2),CBase(a3) { cout << "ChildB constructor!!"
<< endl; } ~ChildB() { cout << "ChildB deconstructor!" << endl; } }; int main()
{ ChildB childb(100,200,300); // Get from ChildA1 Inherited value cout<<" from ChildA1 : a =
"<<childb.ChildA1::GetA(); // Get from ChildA2 Inherited value cout<<" from ChildA2 : a =
"<<childb.ChildA2::GetA()<<endl<<endl; return 0; }
Results of program output :


As you can see from the example above , In class ChildB In the initial list of constructors for , Indirect base class called CBase Constructor for , This is illegal for non base classes , But it is legal and necessary for virtual base class .

   It can be seen from the output , The constructor of its public base class is called only once , Constructor calls that take precedence over non virtual base classes
, And found that , Objects of child derived classes childb Member variable of has only one value , So when the public base class CBase Declared as a virtual base class , Although it became ChildA1 and ChildA2 Public base class of , But child derived class ChildB There is only one backup of it in .



©2019-2020 Toolsou All rights reserved,
Message quality platform series | Full link troubleshooting What is? FPGA? New York Youth Project “ Recapture Wall Street ”: Safeguarding the interests of retail investors Redis Queue implementation java Second kill system , No script , It can be used in production Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood cartoon | CPU Warfare 40 year , The real king finally appeared !JS How to operate 20 Review strategy for senior students with high marks !!! Big data tells you , How tired are Chinese women MySQL An interview is a must !