virtual base class
In the inheritance process that I have learned before, I wonder if you have thought of this situation: in a multiple inheritance relationship, if a derived class inherits from two parent classes, and these two parent classes happen to inherit from one Inherited from the base class. That would be troublesome, because you might inherit two copies of the same member!
This leads to the so-called ambiguity problem. Let’s start studying!
Let me first give you an example that illustrates this kind of multiple inheritance! I thought about it and came up with this example: There used to be a wealthy family. At first, there were two sons. The male master was in charge and kept the key to the door. Later, they had two sons. After a few years, they grew up and the male master was old. , so he matched two more keys and gave them to his two sons respectively, and let them take charge of the family business. After many years, the two sons also got married, and there was a boy and a girl respectively. grandchildren, carrying on the family line. The so-called people are prosperous, and later they have grown up, and it is time to hand over the key. In ancient times, the key was of course passed to the boy, so of course the key was given to the grandson at this time. At this time, the grandson's father and his uncle (that is, the original The owner's two sons) all had to give the grandson a key, so he had two keys. The little grandson looked at the two keys in his hand and said, "One is enough! Two... isn't this a waste?"
After reading this story, it will be easier for everyone to understand inheritance. The original male protagonist in the story, Grandpa, is the base class. His two children are two derived classes, and the child of one of them is the little one. Sun Tzu is the last derived class because he inherits from two derived classes. And these two derived classes have a common member (key, because they both inherit the same base class - grandpa). The last derived class of Sun Tzu has two duplicate members (keys), which is the source of ambiguity!
Below, we follow this example and write the code specifically, you can refer to it:
/************************************//Des: C++ tutorial demo//Author: Huang//Copyright:www.dotcpp.com//Date:2017/12/18********************************** *******/#include<iostream>usingnamespacestd;classGrandfather{public:intkey;public:};classFather1:publicGrandfather{};classFather2:publicGrandfather{};classGrandson:publicFather1,publicFather2{};intmain(){GrandsonA ;//A.key=9;return0;}
The code is a manifestation of the story just now. For the convenience of description, the four defined classes are empty. Just focus on the inheritance relationship. That is, the Grandson class inherits two father classes and will have two key members. If you try to use this key at this time, please note that it has been declared as a public type. When trying to assign a value in the main function, there will be an error message of "not unique, ambiguous" , that is, the so-called ambiguity problem occurs, and everyone can understand it well.
So how to avoid this problem? This is when you need to use virtual base classes ! The so-called virtual base class is to modify it with virtual before inheriting the public type when inheriting. For example, in this example, the father class only needs to add an extra virtual when inheriting the grandfather class. At this time, the derived class and the base class will only maintain A copy of a base class object. Avoid multiple copies and ambiguities.
The definition method is to add a virtual declaration when deriving the two parent classes:
classFather1:virtualpublicGrandfatherclassFather2:virtualpublicGrandfather
The rest remains unchanged. You can test it on your own computer to understand the generation of ambiguity and the usage of virtual base classes.