Virtual Base Classes

Because a class can be an indirect base class to a derived class more than once, C++ provides a way to optimize the way such base classes work. Consider the class hierarchy in Figure 9.5, which illustrates a simulated lunch line.

In Figure 9.5, Queue is the base class for both CashierQueue and LunchQueue. However, when both classes are combined to form LunchCashierQueue, the following problem arises: the new class contains two subobjects of type Queue; one from CashierQueue and the other from LunchQueue. Figure 9.6 shows the conceptual memory layout (the actual memory layout might be optimized).

Note that there are two Queue subobjects in the LunchCashierQueue object. The following code declares Queue to be a virtual base class:

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

};

The virtual keyword ensures that only one copy of the subobject Queue is included (see Figure 9.7).

A class can have both a virtual and a nonvirtual component of a given type. This happens in the conditions illustrated in Figure 9.8.

In Figure 9.8, CashierQueue and LunchQueue use Queue as a virtual base class. However, TakeoutQueue specifies Queue as a base class, not a virtual base class. Therefore, LunchTakeoutCashierQueue has two subobjects of type Queue: one from the inheritance path that includes LunchCashierQueue, and one from the path that includes TakeoutQueue. This is illustrated in Figure 9.9.

Note:

Virtual inheritance provides significant size benefits when compared with nonvirtual inheritance. However, it can introduce extra processing overhead.