0% found this document useful (0 votes)
76 views

Diamond Problem in C++ - CodersLegacy

Educational notes

Uploaded by

hcb Malouvre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Diamond Problem in C++ - CodersLegacy

Educational notes

Uploaded by

hcb Malouvre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

coders@coderslegacy.

com

Search …

Diamond Problem in C++

The Diamond Inheritance Problem in C++ is something that can occur


when performing multiple inheritance between Classes. Multiple
Inheritance is the concept of inheriting multiple classes at once, instead
of just one. If done incorrectly, it can result in the Diamond Problem.

What is the Diamond Inheritance Problem in report this ad


C++?
Search …
The following image illustrates the situation under which the Diamond
problem occurs.

The Class A, is inherited by both Class B and C. Then Class D performs


multiple inheritance by inheriting both B and C. Now why does this cause PROGRAMMING LANGUAGES
a problem?
Learn PYTHON
Learn JAVA
VB.NET
Learn C++

C++
Getting Started with C++
C++ Syntax
C++ Operators
C++ Data Types
C++ Input
C++ Strings
C++ if else statement
C++ Loops
C++ For loop

Well, let’s take a look at another diagram, that represents what actually C++ While Loops

ends up happening. C++ Functions


C++ Switch case statement
C++ File Handling
C++ Arrays
C++ Auto Keyword
C++ Template Types
C++ Structs
C++ Pointers
C++ Smart Pointers
C++ Function Overloading
C++ Virtual Functions
C++ Operator Overloading
C++ Inheritance in Classes
C++ Associative Containers
You may, or may not know, that when inheritance occurs between a
C++ Upcasting and Downcasting
parent and child class, an object of the parent is created and stored
C++ Multithreading Tutorial
alongside the child object. That is how the functions and variables of the
C++ Variadic Templates
parent can be accessed.
C++ Move Semantics Tutorial
C++ Name Mangling
Now if we apply this analogy to the above diagram, you will realize that
the Object D, will end up having 4 more objects with it. Now the number
of objects isn’t the problem, rather the problem is that we have two
objects of Class A alongside Object D. This is a problem, because instead
of one object, we have two objects of Class A. Other than performance
reasons, what effect could this have? Let’s nd out.

Problems caused by Diamond Inheritance


Let’s take a look at the actual code, and observe the problems being
caused. As we mentioned earlier, due to Diamond Inheritance, the Object
D will have two copies of Object A. Let’s run the below code to see proof
of this fact.
1 #include <iostream>
2 using namespace std;
3  
4 class A {
5 public:
6   A()  { cout << "Class A constructor called" <
7 };
8  
9 class B: public A {
10 public:
11   B()  { cout << "Class B constructor called" <
12 };
13  
14 class C: public A {
15 public:
16   C()  { cout << "Class C constructor called" <
17 };
18  
19 class D: public B, public C {
20 public:
21   D()  { cout << "Class D constructor called" <
22 };
23  
24 int main() {
25     D d;
26 }

Class A constructor called


Class B constructor called
Class A constructor called
Class C constructor called
Class D constructor called

As you can see, the Constructor for Class A was called twice, meaning
two objects were created.

Digging Deeper in the Diamond Problem

Let’s take a look at a more detailed example, that shows a situation under
which Diamond Inheritance can actually become a real problem, and
throw an error.

1 #include <iostream>
2 using namespace std;
3  
4 class A {
5     int var;
6 public:
7     A(int n) {
8         cout << "Class A constructor called" <<
9         var = n;
10     }
11  
12     A() { var = 10; }
13  
14     int getVar() { return var; }
15 };
16  
17 class B: public A {
18 public:
19     B(int n) : A(n) {
20         cout << "Class B constructor called" <<
21     }
22 };
23  
24 class C: public A {
25 public:
26     C(int n) : A(n) {
27         cout << "Class C constructor called" <<
28     }
29 };
30  
31 class D: public B, public C {
32 public:
33     D(int n1, int n2) : B(n1), C(n2) {
34         cout << "Class D constructor called" <<
35     }
36 };
37  
38 int main() {
39     D d = D(5, 8);
40     cout << d.B::getVar() << endl;
41     cout << d.C::getVar() << endl;
42 }

Take a good look at this code. What we are doing here, is using the
parameterized constructors to initialize the Objects for Class A. Class B
and C are both passed two values, 5 and 8, which they both use to
initialize to objects for Class A. One of the objects has the value 5, and
one of them has the value 8.

The reason we did this, is so we can highlight the fact that there are two
objects with two different values. Now the question is, when we try to
use the getVar() function on object D, what happens? Normally, if there
were just one object of Class A that was initialized to value “n”, the value
“n” would be returned. But what happens in this case?

1 int main() {
2     D d = D(5, 8);
3     cout << d.B::getVar() << endl;
4     cout << d.C::getVar() << endl;
5 }

Class A constructor called


Class B constructor called
Class A constructor called
Class C constructor called
Class D constructor called
5
8

We can access each of the objects for Class A using the method above.
Where we are performing the getVar() function on both the B and C Class
object that are stored within Object D. As you can see, the value 5 and 8
are printed out.

But what happens when we use the getVar() function on D? Let’s nd out.

1 int main() {
2     D d = D(5, 8);
3     cout << d.getVar() << endl;
4 }

error: request for member 'getVar' is ambiguous


note: candidates are: 'int A::getVar()'
note: 'int A::getVar()'

See this error? It’s happening because it doesn’t know which object to
use. It’s even showing in the error, that there are two objects which the
getVar() function could be applied on.

How to solve the Diamond Problem in C++ using Virtual

The x is very simple. All we need to do, is when inheriting when we make
class B and class C inherit from Class A, we add the virtual keyword after
the colon in the class name.

What this does, is shifts the responsibility of initializing Class A, to Class


D. Previously Class B and C were responsible for initializing it, but since
both were doing it, things became ambiguous. So if only Class D initializes
Class A, there won’t be any problems as only one object is created.
Let’s modify our code now to include the Virtual keyword, and initialize
Class A using Class D. (Although you can choose not to initialize it as well,
and let the default constructor for Class A be used)

1 #include <iostream>
2 using namespace std;
3  
4 class A {
5     int var;
6 public:
7     A(int n) {
8         cout << "Class A constructor called" <<
9         var = n;
10     }
11  
12     A() { var = 10; }
13  
14     int getVar() { return var; }
15 };
16  
17 class B: virtual public A {
18 public:
19     B(int n) : A(n) {
20         cout << "Class B constructor called" <<
21     }
22 };
23  
24 class C: virtual public A {
25 public:
26     C(int n) : A(n) {
27         cout << "Class C constructor called" <<
28     }
29 };
30  
31 class D: public B, public C {
32 public:
33     D(int n1, int n2, int n3) : B(n1), C(n2), A
34         cout << "Class D constructor called" <<
35     }
36 };
37  
38 int main() {
39     D d = D(5, 8, 10);
40     cout << d.getVar() << endl;
41 }
Class A constructor called
Class B constructor called
Class C constructor called
Class D constructor called
10

See our output now? There is no error, and only one object for Class A has
been created. With this, our job is done and we can continue
programming in peace.

This marks the end of the Diamond Problem in C++ Tutorial. Any
suggestions or contributions for CodersLegacy are more than welcome.
Questions regarding the tutorial content can be asked in the comments
section below.

report this ad
Subscribe Login

Be the First to Comment!

0 COMMENTS
Copyright ©2022 CodersLegacy. Education Zone | Developed By Rara Theme. Powered by WordPress. Privacy Policy

You might also like