This is an article in the MSDN I translated, indexes "Virtual Bases".
Because a class can be used as a non-direct base class of derived class, C provides a method of optimizing this base class. The virtual base class provides a method that can save space and avoid uncertainty in the class level of multiple inheritance.
Each non-virtual object contains a copy of the data member defined in the base class. This copy is wasted, and you need you to specify which base class for use when accessing them.
When a base class is specified as a virtual base class, you can work like a non-direct base class without a copy of the data. A copy of a single data member is shared by all base classes used in virtual base classes.
When a virtual base class is declared, the keyword Virtual appears in the list of the base class of the derived class.
Consider the class level as the following figure, it represents an analog lunch queue.
###############
In the figure, Queue is the base class of CashierQueue and LunchQueue; however, when the two class combines the class LunchcashierQueue, the following issues have the following issues: The new class contains two sub-objects of the queue type, one from CashierQueue, and the other From Lunchqueue. The following figure shows the conceptual diagram of the memory layout (the actual memory layout may be optimized):
###############
Note that there are two queue type sub-objects in the LunchcashierQueue object. The following code declares that Queue is a virtual base class:
// deriv_VirtualBaseClasses.cpp class Queue {// Member list}; class CashierQueue: virtual public Queue {// Member list}; class LunchQueue: virtual public Queue {// Member list}; class LunchCashierQueue: public LunchQueue, public CashierQueue {/ / Member List}; int main () {}
Keyword Virtual ensures that only one queue sub-object is included (as shown below).
###########
For the same type, a class can have both 虚 虚 objects, and a non-virtual child object. This can be produced under the conditions shown below.
##########
As shown, CashierQueue and Lunchqueue use Queue as a virtual base class, however, TakeoutQueue specifies Queue as a non-virtual base class. And LunchtakeoutcashierQueue has two sub-objects of the Queue type: a inheritance path from the LunchcashierQueue, one from the inheritance path containing TakeoutQueue. As shown below:
######### Note: The virtual success provides the advantages of providing considerable space compared to non-virtual inheritance. However, it also references additional processing overhead.
If a derived class is defined in inheriting a virtual function in the virtual base class, and if the derived class constructor or the destructor uses a prominent pointer to call this function, the compiler may introduce a hidden name. "VTORDISP" domain. / VD0 Compiler Option Disables the generated hidden VTORDISP constructor. The default / VD1 option is generated when needed. Turn off the VTORDISP only when you affirm all the constructors and fiction functions.
/ VD option affects the entire compilation mode. You can use the VTORDISP precompiled command to disable and then open the VTORDISP domain.
#pragma vtordisp (OFF) Class getReal: Virtual public {...}; #pragma vtordisp (ON)