-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.cpp
More file actions
274 lines (206 loc) · 6.08 KB
/
casting.cpp
File metadata and controls
274 lines (206 loc) · 6.08 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <cstdint>
#include <typeinfo>
#include <cassert>
using namespace std;
/*
IMPORTANT :
Casting VALUES
→ You convert the value 42.19 into another value 42.
Casting POINTERS
→ You do NOT convert the value
→ You change how the same memory bytes are interpreted
Most bugs and confusion come from mixing these two.
Keep this sentence in mind:
Casting a value changes the value.
Casting a pointer changes how memory is read.
*/
// C-Style Cast: Type Safety Loss
void c_style_type_safety()
{
cout << "\n*** C-Style Cast: Loss of Type Safety ***" << endl;
float f = 42.19;
int *p = (int*) &f;
cout << "Float value: " << f << endl;
cout << "Read as int: " << *p << " (garbage!)" << endl;
}
// C-Style Cast: Removing const
void c_style_remove_const()
{
cout << "\n*** C-Style Cast: Removing const ***" << endl;
const int pii = 1337;
int *ptr = (int*)&pii;
cout << "Original: " << *ptr << endl;
*ptr = 42; // Undefined behavior!
cout << "After modification: " << *ptr << endl;
cout << "Warning: This is undefined behavior!" << endl;
}
// static_cast: Value Conversion
void value_conversion()
{
cout << "\n*** static_cast: Value Conversion ***" << endl;
float a = 42.19f;
int b = static_cast<int>(a);
cout << "Float: " << a << endl;
cout << "Int: " << b << " (loses decimal)" << endl;
assert(b == 42);
}
// static_cast: What It Prevents
void static_cast_restrictions()
{
cout << "\n*** static_cast: What It Prevents ***" << endl;
// These won't compile:
// const int a = 42;
// int *ptr = static_cast<int*>(&a); // ERROR: Can't remove const
// int nb = 10;
// float* f = static_cast<float*>(&nb); // ERROR: Can't reinterpret memory
cout << "static_cast prevents:" << endl;
cout << " - Removing const qualifiers" << endl;
cout << " - Memory reinterpretation (int* -> float*)" << endl;
}
struct Base
{
int a = 19;
};
struct Derived : public Base
{
int b = 1337;
};
// Upcasting (Derived -> Base)
void upcasting_example()
{
cout << "\n*** Upcasting (Derived -> Base) ***" << endl;
Derived d;
Base *b = static_cast<Base*>(&d);
cout << "Base::a = " << b->a << endl;
cout << "Same as: " << (*b).a << endl;
assert(b->a == 19);
}
// Downcasting: Safe Case
void downcasting_safe()
{
cout << "\n*** Downcasting (Safe) ***" << endl;
Base *base = new Derived;
Derived *derived = static_cast<Derived*>(base);
cout << "Base part: " << derived->a << endl;
cout << "Derived part: " << derived->b << endl;
assert(derived->a == 19);
assert(derived->b == 1337);
delete base;
}
// Downcasting: Unsafe Case
void downcasting_unsafe()
{
cout << "\n*** Downcasting (Unsafe) ***" << endl;
Base* bas = new Base;
Derived* der = static_cast<Derived*>(bas);
cout << "Base part: " << der->a << " (OK)" << endl;
cout << "Derived part: " << der->b << " (UNDEFINED BEHAVIOR!)" << endl;
delete bas;
}
// reinterpret_cast: Pointer Reinterpretation
void pointer_reinterpretation()
{
cout << "\n*** reinterpret_cast: Pointer Reinterpretation ***" << endl;
int num = 1337;
float* f_ptr = reinterpret_cast<float*>(&num);
cout << "Integer: " << num << endl;
cout << "Reinterpreted as float: " << *f_ptr << " (garbage!)" << endl;
cout << "Note: The bits didn't change, only how we read them" << endl;
}
class clsBase
{
public:
virtual ~clsBase() {}
};
class clsDerived : public clsBase
{
public:
void hej() { cout << "Hej!!!" << endl; }
};
// dynamic_cast: Pointer (returns nullptr on failure)
void dynamic_cast_pointer()
{
cout << "\n*** dynamic_cast: Pointer ***" << endl;
clsBase* b = new clsBase();
clsDerived* d = dynamic_cast<clsDerived*>(b);
if (!d) {
cerr << "Cast failed: " << typeid(*b).name() << " is not clsDerived" << endl;
}
assert(d == nullptr);
delete b;
}
// dynamic_cast: Reference (throws exception on failure)
void dynamic_cast_reference()
{
cout << "\n*** dynamic_cast: Reference ***" << endl;
try {
clsBase *b = new clsBase();
clsDerived &d = dynamic_cast<clsDerived&>(*b);
delete b;
}
catch(const std::bad_cast &e) {
cerr << "Exception caught: " << e.what() << endl;
}
}
void modify(int *ptr)
{
*ptr = 1337;
}
// const_cast: Unsafe (modifying const object)
void const_cast_unsafe()
{
cout << "\n*** const_cast: Unsafe ***" << endl;
const int a = 19;
const int *p = &a;
cout << "a before: " << a << endl;
modify(const_cast<int*>(p)); // Undefined behavior!
cout << "a after: " << a << " (undefined behavior!)" << endl;
}
// const_cast: Safe (object wasn't const)
void const_cast_safe()
{
cout << "\n*** const_cast: Safe ***" << endl;
int x = 1337;
const int& r = x;
int& y = const_cast<int&>(r);
y = 12;
cout << "x after modification: " << x << endl;
assert(x == 12);
}
// Value vs Pointer Casting
void value_vs_pointer()
{
cout << "\n*** Value vs Pointer Casting ***" << endl;
float f = 42.19;
// VALUE casting: converts the value
int value_cast = static_cast<int>(f);
cout << "Value cast (float->int): " << value_cast << " (converts value)" << endl;
// POINTER casting: reinterprets memory
int *ptr_cast = (int*)(&f);
cout << "Pointer cast (float*->int*): " << *ptr_cast << " (reinterprets bits)" << endl;
}
int main()
{
// C-Style Casts
c_style_type_safety();
c_style_remove_const();
// static_cast
value_conversion();
static_cast_restrictions();
// Up and Down Casting
upcasting_example();
downcasting_safe();
downcasting_unsafe();
// reinterpret_cast
pointer_reinterpretation();
// dynamic_cast
dynamic_cast_pointer();
dynamic_cast_reference();
// const_cast
const_cast_unsafe();
const_cast_safe();
//
value_vs_pointer();
return 0;
}