C++ Reference ยท Types and Language Concepts
Multiple and virtual inheritance
Reference for C++ multiple inheritance: several base classes, ambiguity, the diamond problem, virtual inheritance, constructor order and when to use interfaces instead.
A class may inherit from more than one base: class Duck : public Swimmer, public Flyer. The derived object contains one subobject for each base, and it inherits the members of all of them. This is useful for combining independent capabilities, especially when the extra bases are interfaces with only pure virtual functions.
Trouble starts when two bases share a common ancestor (the "diamond"). Without extra care the most-derived object holds two copies of the ancestor, and naming its members becomes ambiguous. Virtual inheritance (: public virtual Base) makes all paths share a single ancestor subobject.
class Duck : public Swimmer, public Flyer { };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
class D : public B1, protected B2, private B3 { ... }; class Mid1 : public virtual Base { ... }; class Most : public Mid1, public Mid2 { Most() : Base(args), Mid1(), Mid2() { } // virtual base initialized here };
| Part | Description |
|---|---|
| Base list | Comma-separated; each base has its own access mode. Bases are constructed in the order listed. |
virtual |
Placed on the intermediate classes' inheritance (: virtual Base). All virtual inheritances of Base in the hierarchy share one subobject. |
| Qualified name | obj.Base1::member or Base1::member() selects among ambiguous members. |
Returns: n/a.
Errors / undefined behavior: using an ambiguous name is a compile error. An ambiguous conversion (derived to a base that appears twice) is a compile error. Casting with static_cast from a virtual base to a derived class is ill-formed (use dynamic_cast).
Usage notes
- Ambiguity resolves explicitly. Qualify the call, add a
usingdeclaration in the derived class, or override the function to combine behavior. - Construction order: virtual bases first (depth-first, left-to-right), then non-virtual bases in declaration order, then members, then the body. Destruction is the reverse.
- The most-derived class initializes the virtual base. Initializer-list calls to the virtual base in intermediate classes are ignored when they are not the final object, as in the second example (
Animal(99), not 1 or 2). - Virtual inheritance costs. Objects are bigger, access to the shared base goes through an extra indirection, and casts are slower. Use it only where a diamond really occurs.
- Interfaces are the safe use. Inheriting from several pure-virtual interfaces and at most one class with data avoids almost all trouble. The standard library's iostream hierarchy (
basic_iostream) is the classic diamond withvirtual basic_ios. - Pointer values differ. With multiple bases, converting
Derived*to a base pointer may adjust the address, so never compare or cast viavoid*orreinterpret_cast. - Slicing and copying work per subobject; the copy constructors of every base run.
- Alternatives: composition (hold a
Swimmermember), templates (mixins and CRTP), or a type-erasure wrapper.