Diamond Problem in C++ - CodersLegacy
Diamond Problem in C++ - CodersLegacy
com
Search …
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
As you can see, the Constructor for Class A was called twice, meaning
two objects were created.
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 }
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 }
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.
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.
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
0 COMMENTS
Copyright ©2022 CodersLegacy. Education Zone | Developed By Rara Theme. Powered by WordPress. Privacy Policy