21.8. Multiple Inheritance and virtual Base Classes

In Section 21.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 basic_iostream (Fig. 21.12).

Image

Fig. 21.12. Multiple inheritance to form class basic_iostream.

Class basic_ios is the base class for both basic_istream and basic_ostream, each of which is formed with single inheritance. Class basic_iostream inherits from both basic_istream and basic_ostream. This enables class basic_iostream objects to provide the functionality of basic_istreams and basic_ostreams. In multiple-inheritance hierarchies, the inheritance described in Fig. 21.12 is referred to as diamond inheritance

Because classes basic_istream and basic_ostream each inherit from basic_ios, a potential problem exists for basic_iostream. Class 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. In this section, you’ll see how using virtual base classes solves the problem of inheriting duplicate copies of an indirect base class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.130.199