-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiamondInheritance.cpp
More file actions
84 lines (68 loc) · 1.45 KB
/
DiamondInheritance.cpp
File metadata and controls
84 lines (68 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* \file DiamondInheritance.cpp
* \brief Virtual inheritance
*
* \see https://www.youtube.com/watch?v=7g8HufwNa0g&t=974s
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
struct A
{
const int value {7};
A()
{
STD_TRACE_FUNC;
}
};
//--------------------------------------------------------------------------------------------------
struct B :
virtual A
{
B()
{
STD_TRACE_FUNC;
}
};
//--------------------------------------------------------------------------------------------------
struct C :
virtual A
{
C()
{
STD_TRACE_FUNC;
}
};
//--------------------------------------------------------------------------------------------------
struct D :
B, C
{
D()
{
STD_TRACE_FUNC;
}
};
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
D d;
std::cout << "\n\t" << STD_TRACE_VAR(d.value) << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
// Without virtual
::: A :::
::: B :::
::: A :::
::: C :::
::: D :::
// d.value - error: request for member ‘value’ is ambiguous
// With virtual
::: A :::
::: B :::
::: C :::
::: D :::
d.value: 7
#endif