-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCasts.cpp
More file actions
255 lines (205 loc) · 7.23 KB
/
Casts.cpp
File metadata and controls
255 lines (205 loc) · 7.23 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// =====================================================================================
// Casts.cpp // static_cast, dynamic_cast, const_cast, reinterpret_cast
// and C-Style casts examples
// =====================================================================================
module modern_cpp:casts;
namespace DiverseCasts {
static void test_01_implicit_conversions()
{
{
// no conversions at all
int a = 123;
long b = 123l;
}
{
// implicit conversions, done by the compiler
long a = 123; // int implicitly converted to long
double b = 123l; // long implicitly converted to double
}
{
// implicit conversions, done by the machine code
int a = 123;
long b = 123l;
long n = a; // int implicitly converted to long
double m = b; // long implicitly converted to double
}
{
// Promotion
long a = 123; // int promoted to long
double b = 123l; // long promoted to double
}
{
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4305)
// Demotion
int a = 10.0; // warning: possible loss of data
bool b = 123; // warning: possible loss of data
#pragma warning(pop)
}
}
static void test_02_explicit_conversions_static_cast()
{
{
// static_cast
int a = static_cast<int>(123.456); // double demoted to int
}
{
// C-Style-Cast
char c = 10; // 1 byte
int* p = (int*) &c; // 4 bytes, works ?!?
// *p = 5; // run-time error: stack corruption
}
{
// static_cast
//char c = 10; // 1 byte
//int* p = static_cast<int*>(&c); // 4 bytes // compile error
}
enum class Color { red, green, blue };
{
// int n = Color::blue; // a value of type "Color" cannot be used to initialize an entity of type "int"
int color = static_cast<int>(Color::blue);
std::println("Color: {}", color);
}
}
static void test_03_explicit_conversions_reinterpret_cast()
{
{
// reinterpret_cast
char c = 10; // 1 byte
int* p = reinterpret_cast<int*>(&c); // 4 bytes // compiles
// *p = 5; // run-time error: stack corruption
}
struct MyStruct
{
char x1;
char x2;
char x3;
char x4;
};
{
struct MyStruct s{ 'A' , 'B' , 'C' , '\0' };
// reinterpret struct as char* pointer :-)
char* ptr = reinterpret_cast<char*>(&s);
std::println("{}", ptr);
}
}
static void print(int* ptr)
{
std::println("{}", *ptr);
}
static void test_04_explicit_conversions_const_cast()
{
{
const int constVar = 123;
int* nonConstIp = const_cast<int*>(&constVar); // removes const
*nonConstIp = 10; // potential run-time error
}
{
const int constVar = 123;
// print(&constVar); // error: cannot convert argument 1 from 'const int*' to 'int*'
print(const_cast<int*>(&constVar)); // allowed
}
}
static void test_05_explicit_conversions_dynamic_cast()
{
class Base
{
public:
virtual void test() { std::println("Base"); }
};
class Derived : public Base
{
public:
void test() override { std::println("Derived"); }
};
{
Derived* child = new Derived();
Base* base = dynamic_cast<Base*>(child); // ok
base->test();
}
{
Base* base = new Base();
Derived* child = dynamic_cast<Derived*>(base);
if (child == 0) {
std::println("Null pointer returned!");
}
}
{
Base base;
try {
Derived& child = dynamic_cast<Derived&>(base);
}
catch (std::bad_cast& e)
{
std::println("{}", e.what());
}
}
{
// less performance overhead : using a static_cast
Derived* child = new Derived();
Base* base = static_cast<Base*>(child); // ok
base->test();
}
{
// conversion may either succeed or fail:
// Failure: the base pointer points to a Base instance
// Success: the base pointer points to a Derived instance
Base* base = new Derived(); // toggle between Base and Derived
Derived* child = dynamic_cast<Derived*>(base);
if (child == 0) {
std::println("Null pointer returned!");
}
else {
std::println("dynamic_cast successful!");
}
}
{
// conversion may either succeed or fail:
// Failure: the base pointer points to a Base instance
// Success: the base pointer points to a Derived instance
Base* base = new Base(); // toggle between Base and Derived
Derived* child = static_cast<Derived*>(base);
if (child == 0) {
std::println("Null pointer returned!");
}
else {
std::println("static_cast successful!"); // Oooops
}
}
{
// upcast: implicit conversion - always allowed
Derived derived;
Base& br = derived;
br.test();
// downcast: old-style cast
Derived& anotherDerived = (Derived&)br;
anotherDerived.test();
// downcast: correct style
Derived& anotherDerived2 = static_cast<Derived&>(br);
anotherDerived2.test();
// downcast: old-style cast
// Take care: You cannot change a reference from the object to which it references once it bound at initialization.
Base anotherBase;
Derived& anotherDerived3 = (Derived&) anotherBase;
anotherDerived3.test();
// downcast: correct style
// Take care: You cannot change a reference from the object to which it references once it bound at initialization.
Derived& anotherDerived4 = static_cast<Derived&>(anotherBase);
anotherDerived4.test();
}
}
}
// -------------------------------------------------------------------
void main_casts()
{
using namespace DiverseCasts;
test_01_implicit_conversions();
test_02_explicit_conversions_static_cast();
test_03_explicit_conversions_reinterpret_cast();
test_04_explicit_conversions_const_cast();
test_05_explicit_conversions_dynamic_cast();
}
// =====================================================================================
// End-of-File
// =====================================================================================