Eliminating Duplicate Subobjects with virtual Base-Class Inheritance

The problem of duplicate subobjects is resolved with virtual inheritance. When a base class is inherited as virtual, only one subobject will appear in the derived class—a process called virtual base-class inheritance. Figure 21.14 revises the program of Fig. 21.13 to use a virtual base class.

 1   // Fig. 21.14: fig21_14.cpp 2   // Using virtual base classes. 3   #include <iostream> 4   using namespace std; 5  6   // class Base definition 7   class Base 8   { 9   public:10      virtual void print() const = 0; // pure virtual11   }; // end class Base12 13   // class DerivedOne definition14   class DerivedOne : virtual public Base15   {16   public ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.