forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex19_3_4.cpp
102 lines (79 loc) · 2.1 KB
/
ex19_3_4.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Exercises Section 19.2.1
Exercise 19.3:
Given the following class hierarchy in which each class defines a public default constructor and virtual destructor:
class A { . . . };
class B : public A { . . . };
class C : public B { . . . };
class D : public B, public A { . . . };
which, if any, of the following dynamic_casts fail?
(a) A *pa = new C;
B *pb = dynamic_cast< B* >(pa);
(b) B *pb = new B;
C *pc = dynamic_cast< C* >(pb);
(c) A *pa = new D;
B *pb = dynamic_cast< B* >(pa);
Solution:
(a) Succeed. The type of ‘pa’ (class type 'C') is publicly derived from the target type 'B'.
(b) Fail. The type of ‘pb’ (class type 'B') is a public base class of the target type 'C'. ‘pc’ will equal to nullptr.
(c) Fail. ‘A’ is an ambiguous base of ‘D’. Converting a pointer of 'D' to a pointer of 'A' is not allowed.
Exercise 19.4:
Using the classes defined in the first exercise, rewrite the following code to convert the expression *pa to the type C&:
if (C *pc = dynamic_cast< C* >(pa))
// use C's members
} else {
// use A's members
}
Solution:
try {
C &rc = dynamic_cast<C&>(ra);
// use C's members
} catch (bad_cast) {
// use A's members
}
*/
#include <iostream>
#include <typeinfo>
using namespace std;
class A {
public:
virtual ~A() {}
};
class B : public A {
public:
virtual ~B() {}
};
class C : public B {
public:
virtual ~C() {}
};
class D : public B, public A {
public:
virtual ~D() {}
};
int main(int argc, char const *argv[])
{
/* Exercise 19.3 */
A *pa = new C;
B *pb = dynamic_cast< B* >(pa);
if (pb) cout << "19.3 (a) succeed!" << endl;
else cout << "19.3 (a) fail!" << endl;
pb = new B;
C *pc = dynamic_cast< C* >(pb);
if (pc) cout << "19.3 (b) succeed!" << endl;
else cout << "19.3 (b) fail!" << endl;
/* pa = new D;
pb = dynamic_cast< B* >(pa); */
/* Exercise 19.4 */
C c; B b;
A &ra1 = c, &ra2 = b;
try {
/* succeed */
C &rc1 = dynamic_cast<C&>(ra1);
cout << "19.4 succeed!" << endl;
/* fail */
C &rc2 = dynamic_cast<C&>(ra2);
} catch (bad_cast) {
cout << "19.4 failed!" << endl;
}
return 0;
}