-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypecast.cpp
205 lines (189 loc) · 4.36 KB
/
typecast.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// #define DEBUG_SHOW_DUMP_CHALLENGE_2
#pragma GCC diagnostic ignored "-Wunused-variable"
// Prevent big size of .class file
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
#include <iostream>
#include <typeinfo>
using namespace std;
class D
{
int i;
public:
D() : i(0) { cout << "D::D()" << endl; }
D(int _i) : i(_i) { cout << "D::D(int)" << endl; }
~D() { cout << "D::~D()" << endl; }
void set(int _i) { i = _i;}
int get() const { return i; }
};
class E : public D {
double d;
public:
E() : d(0.0) { cout << "E::E()" << endl; }
E(double _d) : d(_d) { cout << "E::E(double)" << endl; }
~E() { cout << "E::~E()" << endl; }
void set(double _d) { d = _d;}
double get() const { return d; }
};
// Const cast
void challenge0() {
const D* d = new D(1);
// d->set(2);
D* d1 = const_cast<D*>(d);
d1->set(2);
// *i = 'T';
cout << d1->get() << ", " << d->get() << endl;
delete d;
}
// Static_cast
void challenge1() {
// basic types
int ii = 10;
double test = 10;
double dd = static_cast<double>(ii);
float tmp_f = static_cast<float>(ii);
cout << typeid(ii).name() << "["<< ii << "], "
<< typeid(dd).name() << "["<< dd << "], "
<< typeid(tmp_f).name() << "["<< tmp_f << "], " << test << endl;
// User types
// Upcast
E* e1 = new E(0.1);
D* d1 = static_cast<D*>(e1);
cout << e1->get() << ", " << d1->get() << endl;
delete e1;
// downcast
// undefined behavior
D* d2 = new D(2);
E* e2 = static_cast<E*>(d2);
cout << d2->get() << ", " << e2->get() << endl;
delete d2;
}
class nonVirtual {
public:
nonVirtual() { cout << "nonVirtual::nonVirtual()" << endl; }
~nonVirtual() { cout << "nonVirtual::~nonVirtual()" << endl; }
};
class AnonVirtual : public nonVirtual {
public:
AnonVirtual() { cout << "AnonVirtual::AnonVirtual()" << endl; }
~AnonVirtual() { cout << "AnonVirtual::~AnonVirtual()" << endl; }
};
class BnonVirtual : public AnonVirtual
{
public:
BnonVirtual() { cout << "BnonVirtual::BnonVirtual()" << endl; }
~BnonVirtual() { cout << "BnonVirtual::~BnonVirtual()" << endl; }
};
#endif
/* end define */
class Base {
public:
Base() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "Base::Base()" << endl;
#endif
}
virtual ~Base() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "Base::~Base()" << endl;
#endif
}
virtual void print() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "Base::print()" << endl;
#endif
}
};
class A : public Base {
public:
A() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "A::A()" << endl;
#endif
}
virtual ~A() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "A::~A()" << endl;
#endif
}
virtual void print() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "A::print()" << endl;
#endif
}
};
class B : public A {
public:
B() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "B::B()" << endl;
#endif
}
virtual ~B() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "B::~B()" << endl;
#endif
}
virtual void print() {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "B::print()" << endl;
#endif
}
};
void challenge2() {
// Use non polymorphic classes
// source type is not polymorphic - compile error
// nonVirtual* nv = new nonVirtual();
// BnonVirtual* bnv = dynamic_cast<BnonVirtual*>(nv);
// Use polymorphic
// Upcasting(from derived class to base)
B* b = new B;
Base* tmp_base = new Base;
if (Base* base = dynamic_cast<Base*>(b)) {
base->print();
} else {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "base is null" << endl;
#endif
}
// Crosscasting
if (A* a = dynamic_cast<A*>(b)) {
a->print();
} else {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "a is null" << endl;
#endif
}
// Downcasting
// tmp_b == null
if (B* tmp_b = dynamic_cast<B*>(tmp_base)) {
tmp_b->print();
} else {
#ifndef DEBUG_SHOW_DUMP_CHALLENGE_2
cout << "tmp_b is null" << endl;
#endif
}
delete b;
}
typedef void(B::*pmemory)();
typedef void(Base::*pmemory_polymoph)();
// Pointer to member
void challenge3() {
B* b = new B;
pmemory p = &B::print;
b->print(); // direct call
(b->*p)();
delete b;
// Polymorph classes
Base* base = new B;
pmemory_polymoph pp = &Base::print;
base->print();
(base->*pp)();
delete base;
}
int main() {
// challenge0();
// challenge1();
// challenge2();
challenge3();
return 0;
}