-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcast.cpp
212 lines (164 loc) · 5.49 KB
/
cast.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
206
207
208
209
210
211
212
#include <iostream>
/*
1. Static Cast
It is a compile time cast. static_cast performs a tight type checking.
2. Dynamic Cast,
This cast is executed at runtime, not compile time. It is exclusively used
for handling polymorphism.
You can use it for more than just casting downwards – you can cast sideways
or even up another chain. The dynamic_cast will seek out the desired object and
return it if possible. If it can't, it will return nullptr in the case of a
pointer, or throw std::bad_cast in the case of a reference.
RTTI (Run-time type Information):
In C++, RTTI (Run-time type information) is a mechanism that exposes
information about an object’s data type at runtime and is available only for the
classes which have at least one virtual function. It allows the type of an
object to be determined during program execution
3. Const Cast
//https://www.geeksforgeeks.org/const_cast-in-c-type-casting-operators/
4. Reinterpret Cast
It is used to convert one pointer of another pointer of any type, no matter
either the class is related to each other or not. It does not check if the
pointer type and data pointed by the pointer is same or not.
*/
/////////////////////////////////Static
/// Cast/////////////////////////////////////
class Int {
int x;
public:
// Int(int x_in = 0): x(x_in){}
Int(int x_in = 0) : x{x_in} {}
operator std::string() {
std::cout << "Conversion Operator" << std::endl;
return std::to_string(x);
}
};
void staticCastExmple() {
{ // first example
float f = 3.5;
int a = f; // c style
int b = static_cast<int>(f); // recommended
std::cout << "b: " << b << "a:" << a << std::endl;
}
{ // second example
char c = 'a';
// c style casting,pass at compile time, may fail at run time
int *q = (int *)&c;
std::cout << "q" << *q << std::endl;
// this is will fail in comile time
// int* p = static_cast<int*>(&c);
}
{ // third example
Int obj(3);
// compiler will not thrown an error as we have defined the Conversion
// operator.
std::string str = obj;
obj = 20;
std::string str2 = static_cast<std::string>(obj);
obj = static_cast<Int>(30);
}
}
//////////////////////////////Dynamic Cast///////////////////////////
class Base {
/*
RTTI (Run-time type information) is a mechanism that exposes information about
an object’s data type at runtime and is available only for the classes which
have at least one virtual function.
*/
virtual void f() {}
public:
void whoAmI() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
void whoAmI() { std::cout << "Derived" << std::endl; }
};
class Foo {};
void dynamicCastExample() {
Base base;
Derived derived;
Derived *derived1 = dynamic_cast<Derived *>(
&base); // nullptr, because 'base' is not a 'derived'
std::cout << "derived1 is: "
<< (derived1 == nullptr ? "nullptr" : "object of Derived class")
<< std::endl;
Base *basePointer = new Base;
Derived *derived2 = dynamic_cast<Derived *>(basePointer);
std::cout << "derived2 is: "
<< (derived2 == nullptr ? "nullptr" : "object of Derived class")
<< std::endl;
Base *b = new Derived;
Derived *d = dynamic_cast<Derived *>(b);
std::cout << "d is: "
<< (d == nullptr ? "nullptr" : "object of Derived class")
<< std::endl;
Foo *foo = dynamic_cast<Foo *>(basePointer); // nullptr
std::cout << "foo is: "
<< (foo == nullptr ? "nullptr" : "object of Derived class")
<< std::endl;
Base &baseReference = dynamic_cast<Base &>(*basePointer); // Ok.
// Derived& derivedReference = dynamic_cast<Derived&> (*basePointer); //
// Ok.
// this will create a std::bad_cast
// Foo& fooReference = dynamic_cast<Foo&> (*basePointer);
}
/////////////////////////////////Const Cast///////////////////////////////////
/////////////////////////////Reinterpret Cast/////////////////////////////////
struct mystruct {
int x;
int y;
char c;
bool b;
};
void reinterpretCastExample() {
{
int *p = new int(65);
char *ch = reinterpret_cast<char *>(p);
std::cout << *p << std::endl;
std::cout << *ch << std::endl;
std::cout << p << std::endl;
std::cout << ch << std::endl;
}
{
mystruct s;
// Assigning values
s.x = 5;
s.y = 10;
s.c = 'a';
s.b = true;
// data type must be same during casting
// as that of original
// converting the pointer of 's' to,
// pointer of int type in 'p'.
int *p = reinterpret_cast<int *>(&s);
std::cout << sizeof(s) << std::endl;
// printing the value currently pointed by *p
std::cout << *p << std::endl;
// incrementing the pointer by 1
p++;
// printing the next integer value
std::cout << *p << std::endl;
p++;
// we are casting back char * pointed
// by p using char *ch.
char *ch = reinterpret_cast<char *>(p);
// printing the character value
// pointed by (*ch)
std::cout << *ch << std::endl;
ch++;
/* since, (*ch) now points to boolean value,
so it is required to access the value using
same type conversion.so, we have used
data type of *n to be bool. */
bool *n = reinterpret_cast<bool *>(ch);
std::cout << *n << std::endl;
// we can also use this line of code to
// print the value pointed by (*ch).
std::cout << *(reinterpret_cast<bool *>(ch));
}
}
int main() {
// staticCastExmple();
dynamicCastExample();
// reinterpretCastExample();
}