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.
[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 ClassesIn Section 24.7, we discussed multiple inheritance, the process by which one class inherits from two or more classes. Multiple inheritance is used, for example, in the C++ standard library to form class
Fig. 24.12 Multiple inheritance to form class
basic_ios is the base class for both
basic_ostream, each of which is formed with single inheritance. Class
basic_iostream inherits from both
basic_ostream. This enables objects of class
basic_iostream to provide the functionality of both
basic_ostreams. In multiple-inheritance hierarchies, the situation described in Fig. 24.12 is referred to as diamond inheritance.
basic_ostream each inherit from
basic_ios, a potential problem exists for
basic_iostream could contain two copies of the members of class
basic_ios—one inherited via class
basic_istream and one inherited via class
basic_ostream). Such a situation would be ambiguous and would result in a compilation error, because the compiler would not know which version of the members from class
basic_ios to use. Of course,
basic_iostream does not really suffer from the problem we mentioned. In this section, you will see how using
virtual base classes solves the problem of inheriting duplicate copies of an indirect base class.
Compilation Errors Produced When Ambiguity Arises in Diamond Inheritance
Figure 24.13 demonstrates the ambiguity that can occur in diamond inheritance. The program defines class
Base (lines 9–13), which contains pure
DerivedOne (lines 16–24) and
DerivedTwo (lines 27–35) each publicly inherit from class
Base and override the
DerivedOne and class
DerivedTwo each contain what the C++ standard refers to as a base-class subobject—i.e., the members of class
Base in this example.
|Fig. 24.13||Attempting to call a multiply inherited function polymorphically.|
C:\Projects\cpphtp5\examples\ch24\Fig24_20\Fig24_20.cpp(55): error C2594: '=' : ambiguous conversions from 'Multiple *' to 'Base *'
Multiple (lines 38–46) inherits from both classes
DerivedTwo. In class
DerivedTwo to specify which version of
main(lines 48–64) declares objects of classes
DerivedOne(line 51) and
DerivedTwo(line 52). Line 53 declares an array of
Base *pointers. Each array element is initialized with the address of an object (lines 55–57). An error occurs when the address of
both—an object of class
Multiple—is assigned to
array[ 0 ]. The object
bothactually contains two subobjects of type
Base, so the compiler does not know which subobject the pointer
array[ 0 ]should point to, and it generates a compilation error indicating an ambiguous conversion.
Other tutorials on this topicl:
24.7 Multiple Inheritance
24.8 Multiple Inheritance and
virtual Base Classes (You are here).