Multiple inheritance
Multiple inheritance lets a class list more than one base: class D : public A, public B. D contains both bases' subobjects. The diamond problem appears when two bases share a grandparent; virtual bases merge that grandparent into one.
Detailed Explanation
Java allows one class parent and many interfaces. C++ allows many class parents. That is useful when you compose two real implementations: a LoggedFile that is a File and a Logger, or a widget that is both Drawable and Clickable with code in each. It is also how C++ expresses interface-like bases: empty classes with pure virtual functions, mixed with one concrete base.
Layout is concatenation plus padding. A D object starts with an A subobject (or a vptr, depending on the ABI and the order of bases), then a B subobject. static_cast<A*>(d) and static_cast<B*>(d) can produce different addresses. That is why a C-style cast between sibling bases is dangerous, and why static_cast / dynamic_cast exist.
Name clashes are resolved with qualification. If A and B both have open(), d.open() is ambiguous. Write d.A::open() or add a using and a forwarding function in D that picks one, or wraps both.
The diamond: class B : public A {}; class C : public A {}; class D : public B, public C {};. A D then contains two A subobjects. d.A::x is ambiguous even with qualification through A alone; you need d.B::x or a virtual base. class B : virtual public A and class C : virtual public A merge the A into one subobject. Construction of a virtual base is done by the most derived class, not by B or C. If you forget that, A's default constructor runs and your B(A args) initialiser is ignored for the shared A.
Virtual bases cost a layer of indirection (a vbptr or equivalent) so the shared subobject can sit at a runtime-known offset. Use them when the diamond is real. Do not make every base virtual "just in case".
When multiple inheritance is the right tool: mixin classes that provide implementations (std::enable_shared_from_this), combining an interface with a concrete type, or adapting two C++ libraries that each require you to inherit. When it is the wrong tool: modelling "is-a" twice for types that should be composed ("a car is an engine and a radio" is a has-a). Prefer a member.
C++ access with multiple bases follows each path. public A, private B makes A's public interface part of D's public interface and hides B. Construction order is the order of base specifiers, virtual bases first. Destruction is the reverse.
dynamic_cast across a multiple-inheritance lattice is the safe way to ask "is this A* also a C*?" when you are not sure. It adjusts the pointer or returns null.
Code Examples
#include <iostream>
struct Drawable {
virtual ~Drawable() = default;
virtual void draw() const = 0;
};
struct Clickable {
virtual ~Clickable() = default;
virtual void click() = 0;
};
struct Button : Drawable, Clickable {
void draw() const override { std::cout << "draw\n"; }
void click() override { std::cout << "click\n"; }
};
int main() {
Button b;
Drawable* d = &b;
Clickable* c = &b;
d->draw(); // prints: draw
c->click(); // prints: click
// d and c may store different addresses
}#include <iostream>
struct A { int id = 1; };
struct B : virtual A { B() { id = 2; } };
struct C : virtual A { C() { id = 3; } };
struct D : B, C {
D() { id = 4; } // most derived initialises the one A
};
int main() {
D d;
std::cout << d.id << '\n'; // prints: 4
}Common Gotchas
Two non-virtual paths to the same base
`D` has two `A` subobjects. `A*` from a `B*` and `A*` from a `C*` are different objects. Use `virtual` inheritance if you wanted one.
Most-derived constructs the virtual base
`B(int n) : A(n) {}` does not run when `B` is a subobject of `D`. Initialise `A` in `D`'s constructor.
Ambiguous member names
two `size()` methods from two bases will not compile at the call. Write a forwarding member in the derived class.
Pointer adjustment is real
`reinterpret_cast<Clickable*>(drawable)` is undefined if the `Clickable` subobject is not at offset 0. Use `static_cast` when the type is known, `dynamic_cast` when it is not.