-
Notifications
You must be signed in to change notification settings - Fork 160
/
inheritance_override.cpp
66 lines (56 loc) · 1.12 KB
/
inheritance_override.cpp
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
// Let's learn how method name resolution works with inheritance.
#include "common.hpp"
class B {
public:
int i;
int iAmbiguous;
int f() {
return 0;
}
int fAmbiguous() {
return 0;
}
};
class B2 {
public:
int iAmbiguous;
int fAmbiguous() {
return 1;
}
};
class C : public B, public B2 {
public:
int i;
int f() {
return 2;
}
};
int main() {
C c;
C *cp = &c;
// Refer to members of base vs derived class with same name.
c.i = 0;
c.C::i = 0;
cp->C::i = 0;
c.B::i = 1;
assert(c.i == 0);
assert(c.C::i == 0);
assert(cp->C::i == 0);
assert(c.B::i == 1);
assert(cp->B::i == 1);
#if 0
// ERROR: ambiguous because on multiple base classes.
c.iAmbiguous = 0;
#endif
c.B::iAmbiguous = 0;
// Analogous for methods.
callStack.clear();
assert(c.f() == 2);
assert(c.B::f() == 0);
#if 0
// ERROR ambiguous.
c.fAmbiguous();
#endif
assert(c.B::fAmbiguous() == 0);
assert(c.B2::fAmbiguous() == 1);
}