Skip to main content

Lesson 4 of 6

Multiple Inheritance & the Diamond Problem

C++ allows inheriting from multiple base classes. The **diamond problem** occurs when two base classes share a common ancestor: `D` inherits from `B` and `C`, both of which inherit from `A`. Without `virtual` inheritance, `D` has two copies of `A`. `virtual` inheritance ensures only one shared copy of `A` exists. In modern C++, prefer **interface-style** (pure abstract) multiple inheritance over data-bearing multiple inheritance.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Diamond Problem, virtual Inheritance & Mixin Pattern

Diamond Problem, virtual Inheritance & Mixin Pattern
#include <iostream>
#include <string>

// ── Diamond problem ───────────────────────────────────────────
struct Base {
    int value = 0;
    void identify() { std::cout << "Base::value=" << value << "
"; }
};

struct Left  : Base { int left_data  = 1; };  // has a Base copy
struct Right : Base { int right_data = 2; };  // has another Base copy

// struct Diamond : Left, Right {  // TWO copies of Base!
//     void demo() {
//         value = 5;          ← AMBIGUOUS: Left::Base::value or Right::Base::value?
//         Left::value  = 5;   ← works but messy
//         Right::value = 5;
//     }
// };

// ── Virtual inheritance - one shared Base ─────────────────────
struct VLeft  : virtual Base { int left_data  = 1; };
struct VRight : virtual Base { int right_data = 2; };

struct Diamond : VLeft, VRight {
    void demo() {
        value = 42;   // ONE shared Base::value - unambiguous
        left_data  = 10;
        right_data = 20;
        identify(); // outputs 42
    }
};

// ── Preferred: multiple interface inheritance ─────────────────
// Pure abstract interfaces have no data → no diamond problem
class ISaveable {
public:
    virtual ~ISaveable() = default;
    virtual void save(const std::string& path) const = 0;
};

class ILoadable {
public:
    virtual ~ILoadable() = default;
    virtual void load(const std::string& path) = 0;
};

// ── Mixin pattern - add behaviour via inheritance ─────────────
// Mixin: a class designed to add behaviour with no data, no virtual
template<typename T>
class Printable {
public:
    void print() const {
        std::cout << static_cast<const T*>(this)->to_string() << "
";
    }
};

class Point : public Printable<Point> {
public:
    double x, y;
    Point(double x, double y) : x(x), y(y) {}
    std::string to_string() const {
        return "Point(" + std::to_string(x) + ", " + std::to_string(y) + ")";
    }
};

class Document : public ISaveable, public ILoadable, public Printable<Document> {
    std::string content_;
public:
    explicit Document(std::string c) : content_(std::move(c)) {}
    void save(const std::string& path) const override {
        std::cout << "Saving to " << path << ": " << content_ << "
";
    }
    void load(const std::string& path) override {
        content_ = "loaded from " + path;
    }
    std::string to_string() const { return "Doc: " + content_; }
};

int main() {
    Diamond d;
    d.demo();   // Base::value=42

    Point p{3.0, 4.0};
    p.print();  // Point(3.000000, 4.000000)

    Document doc{"Hello"};
    doc.save("/tmp/test.txt");
    doc.print();
    doc.load("/tmp/test.txt");
    doc.print();

    return 0;
}

Common Mistakes

  • Mixing virtual and non-virtual inheritance of the same base - if `Left` uses `virtual Base` but `Right` uses plain `Base`, the resulting diamond still has two Base copies (one shared from Left's virtual, one separate from Right's non-virtual). All paths to a base must be `virtual` for the sharing to work.
  • Forgetting that virtual base construction is the most-derived class's responsibility - in virtual inheritance, the most-derived class (`Diamond`) must call `Base`'s constructor directly. If `Diamond` has a constructor, it must explicitly call `Base(args)` in its MIL - `Left` and `Right`'s Base-constructor calls are ignored.
  • Over-using multiple inheritance with data - data-bearing multiple inheritance + diamond = complex layout decisions and bugs. Prefer: one concrete base (public) + zero or more pure-abstract interfaces (public). This is the Java/C# pattern and is simple and safe.

Tip

Tip

Practice Multiple Inheritance the Diamond Problem in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Resolving duplicate base subobjects and ambiguous member access via virtual base classes

Practice Task

Note

Practice Task - (1) Write a working example of Multiple Inheritance the Diamond Problem from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Multiple Inheritance the Diamond Problem is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Key Takeaways

  • C++ allows inheriting from multiple base classes.
  • Mixing virtual and non-virtual inheritance of the same base - if `Left` uses `virtual Base` but `Right` uses plain `Base`, the resulting diamond still has two Base copies (one shared from Left's virtual, one separate from Right's non-virtual). All paths to a base must be `virtual` for the sharing to work.
  • Forgetting that virtual base construction is the most-derived class's responsibility - in virtual inheritance, the most-derived class (`Diamond`) must call `Base`'s constructor directly. If `Diamond` has a constructor, it must explicitly call `Base(args)` in its MIL - `Left` and `Right`'s Base-constructor calls are ignored.
  • Over-using multiple inheritance with data - data-bearing multiple inheritance + diamond = complex layout decisions and bugs. Prefer: one concrete base (public) + zero or more pure-abstract interfaces (public). This is the Java/C# pattern and is simple and safe.

Topics in This Module

0/6 done