As a C++ lead, would you restrict multiple inheritance to interface-style abstract bases, and which exceptions would your team's rule allow?
answer
- state versus contract
- at most one base with data
- diamonds of state need virtual bases
- name the reviewed exceptions
basics
~20 sUsually yes as the default: any number of pure interface bases, at most one base that carries state or implementation, and virtual inheritance only after review. Allow named exceptions, such as empty tag or policy bases and established library types like std::enable_shared_from_this.
solid answer
~50 sI would make it the default, because nearly every multiple-inheritance hazard in C++ comes from **state** in more than one base: duplicate subobjects in a diamond, virtual bases whose construction the most derived class must own, pointer offsets that break `void*` and `reinterpret_cast` boundaries, and name clashes between implementation bases. Interface-style bases, with pure virtual functions, no data and a virtual or protected destructor, avoid most of that. The rule I would write: any number of interface bases, **at most one** base with data members, and **virtual inheritance only with a design review**. Named exceptions: empty tag or policy bases, `std::enable_shared_from_this` as a second base, types that must slot into an existing library hierarchy such as iostreams, and generated code. The cost is some forwarding boilerplate when composition replaces a second implementation base; I would accept that, and revisit the rule if it starts producing large adapter classes.
go deeper
Know the difference between an interface-style base, with pure virtual functions and no data, and a base that carries state.
Explain which multiple-inheritance problems come from state in more than one base and which ones interface bases avoid.
Show how you would review a class with two stateful bases or a virtual base, and what you would ask its author to change.
Write the rule, its exceptions and its enforcement, weigh boilerplate and ABI costs, and say what evidence would make you revise it.
## What the decision is really about C++ allows a class to inherit any number of bases, with any mix of data, implementation and virtual inheritance. The language does not distinguish an interface from an implementation base; a team rule has to. The lead's decision is not "is multiple inheritance good?" but **which kinds of multiple inheritance the team accepts by default**, which ones need a reviewer, and how that is enforced. ## Where the hazards come from Almost every multiple-inheritance problem a C++ team meets traces back to **state or implementation** in more than one base: - **Duplicate subobjects.** Two stateful middle classes sharing an ancestor give the bottom class two copies, with ambiguous access and state that can diverge. - **Virtual base construction.** Fixing the duplicate with virtual inheritance means the most derived class constructs the shared base, and every future derived class inherits that obligation. - **Pointer adjustment.** Several non-empty bases sit at different offsets; any `void*`, `reinterpret_cast` or integer round trip that assumes otherwise is undefined behaviour. - **Name collisions.** Two implementation bases that both define `update()` or `id()` make unqualified calls ambiguous and invite "which one did I mean?" bugs. - **Fragile coupling.** A class that inherits implementation from two places depends on both bases' protected members and invariants. Interface-style bases, with only pure virtual functions, no data and a virtual or protected destructor, remove most of these. They still cause pointer offsets, but they carry no state to duplicate, need no virtual inheritance, and the derived class owns every behaviour. ## A rule worth writing down A version that works for most application code: 1. A class may inherit **any number of interface-style bases**: pure virtual member functions, no non-static data members, a virtual (or protected non-virtual) destructor. 2. A class may inherit **at most one base that has data members or non-pure virtual functions**. 3. **Virtual inheritance requires a design review**, and the review checks who constructs the virtual base and whether it can be stateless. 4. Code that erases a type to `void*` converts to the exact receiving type first. Named exceptions, each with a reason: | Exception | Why it is acceptable | |---|---| | Empty tag or policy bases | no data, no virtual functions; usually zero size | | `std::enable_shared_from_this<T>` as an extra base | a standard-library base meant to be inherited beside others, even though it holds a `weak_ptr` | | Deriving from library hierarchies that are already diamonds (iostreams) | the library made the design decision | | Generated code, framework-required bases | the generator or framework owns the layout | ## The counter-arguments a lead should weigh - **Boilerplate.** Replacing a second implementation base with a member means forwarding functions. If the forwarding layer grows large, the rule is costing more than it saves for that class. - **Domain fit.** Some domains model genuine "is both" objects with shared behaviour, for example a stream that is both readable and writable with one buffer. A reviewed diamond can be the honest model there. - **Alternatives exist.** Composition, templates with static polymorphism, type erasure and `std::variant` each solve parts of the problem. They are not free either, and they belong to other lessons; the rule should point to them rather than forbid every alternative. - **Hot code and ABI.** On common ABIs each additional polymorphic base adds a pointer-sized virtual table pointer to the object, and every call through an interface is an indirect call. For a stable binary interface, adding a base to an exported class changes layout, so library teams often freeze base lists more strictly than application teams. ## Enforcing and revisiting it A rule nobody checks is a suggestion. Enforcement usually combines **code review checklists**, a static-analysis check where the team's toolchain offers one, and a short rationale in the style guide so reviewers can explain the "why". Revisit the rule when the exceptions list keeps growing, when composition boilerplate shows up in review as a recurring complaint, or when the codebase starts exposing classes across a binary interface. ## How to answer in the interview State a default, justify it by the concrete hazards, name the exceptions with reasons, admit the cost, and say what evidence would make you change it. That shape, not a yes or no, is what the question is testing.
- Why does a C++ rule that allows only interface-style bases rarely need virtual inheritance?Virtual inheritance exists to share one copy of a base's state across a diamond. An interface-style base has no data members, so a class reaching it twice holds two copies of nothing but a virtual table pointer, and a single override still satisfies both paths. Converting the class to that interface is then ambiguous, which is the main reason left to reach it only once. The duplicate-state and construction problems that virtual inheritance solves disappear, so the rule can require a review for it.
- When would you grant an exception for a second stateful base in C++?When the base is a library mixin designed for it, such as `std::enable_shared_from_this`; when the class must join a hierarchy that already has that shape, such as iostreams; or when a review shows composition would force a large forwarding layer for no safety gain. The exception should be recorded with its reason so the next reviewer does not reopen it.
- Multiple inheritance is always harmful in C++, so a team should ban it outright.
- Interface-only bases remove every pointer-offset concern.
- Virtual inheritance should be the default for every base class.
- A team rule needs no exceptions if it is written carefully enough.