Multiple Inheritance & the Diamond Problem
Multiple inheritance
class Printable { public: virtual void print() const = 0; virtual ~Printable() = default; };
class Saveable { public: virtual void save() const = 0; virtual ~Saveable() = default; };
class Invoice : public Printable, public Saveable { ... };
✅ Very common and safe for interfaces (abstract classes without data).
Name ambiguity
If both bases have a member with the same name:
obj.log(); // ❌ ambiguous
obj.A::log(); // ✅ say which one
Or override it in the derived class to resolve it.
The diamond problem
Device
/ \
Scanner Printer
\ /
MultiFunction
Without special handling, MultiFunction contains two separate Device sub-objects — two serial_numbers, two power states. mf.serial_number is ambiguous.
Virtual inheritance
class Scanner : virtual public Device { ... };
class Printer : virtual public Device { ... };
class MultiFunction : public Scanner, public Printer { ... };
Now there's only one shared Device.
Rule with virtual bases ⚠️
The most derived class constructs the virtual base directly:
MultiFunction(...) : Device(serial), Scanner(...), Printer(...) {}
Scanner's and Printer's calls to Device(...) are ignored when building a MultiFunction.
Costs
Virtual inheritance adds hidden pointers/offsets and makes casts and construction more complex.
Advice ✅
- Multiple inheritance of interfaces: fine
- Multiple inheritance of implementation with data: avoid; prefer composition
- Need a diamond? Reconsider the design first
Java and C# allow only one base class but many interfaces — essentially the "safe" subset of C++ multiple inheritance.
Example
#include <iostream>
#include <string>
class Printable {
public:
virtual ~Printable() = default;
virtual void print() const = 0;
};
class Saveable {
public:
virtual ~Saveable() = default;
virtual void save() const = 0;
};
class Invoice : public Printable, public Saveable { // two interfaces: safe and common
public:
explicit Invoice(int no) : no_(no) {}
void print() const override { std::cout << "Printing invoice #" << no_ << '\n'; }
void save() const override { std::cout << "Saving invoice #" << no_ << '\n'; }
private:
int no_;
};
class Device {
public:
explicit Device(std::string serial) : serial_(std::move(serial)) {
std::cout << " Device(" << serial_ << ") constructed\n";
}
const std::string& serial() const { return serial_; }
private:
std::string serial_;
};
class Scanner : virtual public Device {
public:
Scanner() : Device("SCANNER-DEFAULT") {} // ignored when part of a MultiFunction
void scan() const { std::cout << " scanning on " << serial() << '\n'; }
};
class Printer : virtual public Device {
public:
Printer() : Device("PRINTER-DEFAULT") {}
void print_page() const { std::cout << " printing on " << serial() << '\n'; }
};
class MultiFunction : public Scanner, public Printer {
public:
explicit MultiFunction(const std::string& serial) : Device(serial) {} // most derived builds the virtual base
};
void print_it(const Printable& p) { p.print(); }
void save_it(const Saveable& s) { s.save(); }
int main() {
Invoice inv(2026);
print_it(inv);
save_it(inv);
std::cout << "Building a MultiFunction device:\n";
MultiFunction mf("MF-7781");
mf.scan();
mf.print_page();
std::cout << " one shared serial: " << mf.serial() << '\n';
std::cout << " same Device object? " << std::boolalpha
<< (static_cast<const Device*>(static_cast<const Scanner*>(&mf)) ==
static_cast<const Device*>(static_cast<const Printer*>(&mf))) << '\n';
return 0;
}
/* Output:
Printing invoice #2026
Saving invoice #2026
Building a MultiFunction device:
Device(MF-7781) constructed
scanning on MF-7781
printing on MF-7781
one shared serial: MF-7781
same Device object? true
*/