Today, a special look at the virtual inheritance of things, has not been used before, specific as follows:
Parent class:
Copy Code code as follows:
Class Cparent
{
....
};
The declaration of an inheriting class is more specific:
Class Cchild:virtual Public cparent
{
....
}
excuse me, what is the function and meaning of this "virtual"?
---------------------------------------------------------------
Represents virtual inheritance, and normal inheritance is the two ways in which C + + inherits.
For example B1, B2 inherits A and C multiple inheritance B1, B2
If normal inheritance C contains two copies of a, from B1, B2
And virtual inheritance contains only a copy of a
---------------------------------------------------------------
What is the role and meaning of this "virtual"?
Proving that this cparent is a virtual base class for Cchild.
The role of virtual base classes
Virtual base class refers to: Class Subclass:virtual public BaseClass the base class declared in virtual!! Because C + + supports multiple inheritance, there are several direct parent classes in a derived class. Several direct parent classes may inherit from a base class (that is, the parent class of the parent class), so that when the final derived class is constructed, there are several instances of the same base class in the resulting derived class. Creates a two-point question (which does not know which base class's member variables and functions to call), to resolve this problem, you need to use a virtual base class, that is, to generate only one area of memory for the base class, so that only a base class is included in the resulting derived class.
The typical need for virtual base classes is as follows:
A
/ \
B C
\ /
D
where D inherits from BC,BC and inherits from a, so a is to be inherited by BC, respectively.
The procedure is ... .....
Copy Code code as follows:
Class A {
Public
void PrintA () {cout<< "This is a\n";}
};
Class B:virtual public A;
Class C:virtual public A;
Class D:public B,public C;
So after the D is constructed, there is only a a in the storage area, no two semantic problems
For example: D d=new D;
At this point if the use of D.printa (), there will be no problem, but if B and C is not virtual inherit from a, there will be two semantic problems
Multiple inheritance in COM does not use virtual inheritance, or it can result in vtbl that are incompatible with COM. If IX and IY inherit IUnknown according to Virtual inheritance, the first three functions in the VTBL of IX and IY will not point to three member functions of IUnknown (see COM Technical Insider)
IUnknown-> IX
---> CA
IUnknown-> IY
Typically, converting one type of pointer to another type of pointer does not change the value of the pointer, but in order to support multiple inheritance, in some cases, C + + must change the value of the class pointer. This is the case for multiple interface inheritance in COM, where
ca* PA = new CA ();
IY * pc = PA; will be changed by the compiler to IY * PC = (char *) PA + deltaiy (an offset);
The destructor of a base class should also be defined as virtual to purge resources used by the base class when an instance of an inherited class is destructor.