📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page

Was this helpful?

  1. Chapter 7: Multiple Inheritance

Diamond Problem

The benefit of multiple inheritance is that more code can be reused in this inheritance structure. But it brings up another problem, which is known as diamond problem. Let's look at this example.

Here we have a base class A, and two derived class B and C that inherit from it. Then we have another class that multiply inherits from B and C.

class A {
public:
    A(int data) : ma(data) {
        cout << "A()" << endl;
    }
    ~A() {
        cout << "~A()" << endl;
    }
protected:
    int ma;
};
​
class B : public A {
public:
    B(int data) : A(data), mb(data) {
        cout << "B()" << endl;
    }
    ~B() {
        cout << "~B()" << endl;
    }
protected:
    int mb;
};
​
class C : public A {
public:
    C(int data) : A(data), mc(data) {
        cout << "C()" << endl;
    }
    ~C() {
        cout << "~C()" << endl;
    }
protected:
    int mc;
};
​
class D : public B, public C {
public:
    D(int data) : B(data), C(data), md(data) {
        cout << "D()" << endl;
    }
    ~D() {
        cout << "~D()" << endl;
    }
protected:
    int md;
};
​
int main() {
    D d(10);
    return 0;
}

Now in the main function we defined an object of class D. The output shows as follow:

A()
B()
A()
C()
D()
~D()
~C()
~A()
~B()
~A()

We can find that the constructor and destructor of class A have been called twice. Therefore, there are multiple copies of class A's member ma in D as well.

This is apparently a waste of resources, and may cause ambiguity in using the base class's member variables. This problem is called the diamond problem, which happens in two common inheritance structures:

Virtual inheritance is used to solve these kinds of problems in multiple inheritance. Here we can use virtual inheritance in class B and C. In this case, class A is a virtual base class.

class B : virtual public A {
public:
    B(int data) : A(data), mb(data) {
        cout << "B()" << endl;
    }
    ~B() {
        cout << "~B()" << endl;
    }
protected:
    int mb;
};
​
class C : virtual public A {
public:
    C(int data) : A(data), mc(data) {
        cout << "C()" << endl;
    }
    ~C() {
        cout << "~C()" << endl;
    }
protected:
    int mc;
};

Remember that in virtual inheritance, the members of the base class is moved to the end of the memory, and their original locations are replaced with a vbptr which points to the vbtable. Now in class D we only have one copy of ma, and two vbptrs that point to the vbtables for class B and class C, respectively. There are no more repeated members here.

Since ma is moved to the end of the memory, it is no longer within the scope of B:: or C:: anymore. Instead, it is now within the scope of class D. Therefore, ma is required to be initialized by D itself:

class D : public B, public C {
public:
    D(int data) : A(data), B(data), C(data), md(data) {
        cout << "D()" << endl;
    }
    ~D() {
        cout << "~D()" << endl;
    }
protected:
    int md;
};
PreviousVirtual Inheritance and Virtual Base ClassesNextFour Kinds of Type Conversions

Last updated 4 years ago

Was this helpful?