Our C++ Multiple Inheritance tutorial introduced C++'s multiple inheritance capabilities. This tutorial continues our presentation of multiple inheritance by investigating the so-called diamond inheritance problem. We discuss how this problem can be solved with virtual base classes. This tutorial is intended for students and developers who are already familiar with multiple inheritance in C++ or who have read our tutorial on Multiple Inheritance.
Download the code examples for this tutorial.
[Note: This tutorial is an excerpt (Section 24.8) of Chapter 24, Other Topics, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.1218-1222. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
24.8 Multiple Inheritance and virtual Base Classes (Continued)
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. FFigure 24.14 revises the program of Fig. 24.13 to use a virtual base class.
|
||
| Fig. 24.14 | Using virtual base classes. |
|
Output
DerivedTwo DerivedOne DerivedTwo |
The key change in the program is that classes DerivedOne (line 15) and DerivedTwo (line 26) each inherit from class Base by specifying virtual public Base. Since both of these classes inherit from Base, they each contain a Base subobject. The benefit of virtual inheritance is not clear until class Multiple inherits from both DerivedOne and DerivedTwo (line 37). Since each of the base classes used virtual inheritance to inherit class Base’s members, the compiler ensures that only one subobject of type Base is inherited into class Multiple. This eliminates the ambiguity error generated by the compiler in Fig. 24.13. The compiler now allows the implicit conversion of the derived-class pointer (&both) to the base-class pointer array[ 0 ] at line 56 in main. The for statement at lines 61–62 polymorphically calls print for each object.
Constructors in Multiple-Inheritance Hierarchies with virtual Base Classes
Implementing hierarchies with virtual base classes is simpler if default constructors are used for the base classes. The examples in Fig. 24.13 and Fig. 24.14 use compiler-generated default constructors. If a virtual base class provides a constructor that requires arguments, the implementation of the derived classes becomes more complicated, because the most derived class must explicitly invoke the virtual base class’s constructor to initialize the members inherited from the virtual base class.
![]() |
Software Engineering Observation 24.5 |
Providing a default constructor for virtual base classes simplifies hierarchy design. |
Additional Information on Multiple Inheritance
Multiple inheritance is a complex topic typically covered in more advanced C++ texts. The following URLs provide additional information about multiple inheritance.
http://cplus.about.com/library/weekly/aa121302a.htm
A tutorial on multiple inheritance with a detailed example.
http://cpptips.hyperformix.com/MultipleInher.html
Provides technical tips that explain several issues regarding multiple inheritance.
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html
Part of the C++ FAQ Lite. Provides a detailed technical explanation of multiple inheritance and virtual inheritance.
Other tutorials on this topicl:
24.7 Multiple Inheritance
24.8 Multiple Inheritance and virtual Base Classes (You are here).


