-
Notifications
You must be signed in to change notification settings - Fork 0
/
casting.cpp
37 lines (32 loc) · 932 Bytes
/
casting.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
class Base
{
public:
Base() {}
virtual ~Base() {}
};
class Derived : public Base
{
public:
Derived() {}
~Derived() {}
};
class Another
{
public:
Another() {}
~Another() {}
};
int main()
{
double x = 5.5;
double y = 5;
double c = (int)x + y; // C style cast
// C++ style casts (adds compile time checks)
Derived *derived = new Derived();
Base *static_casted = static_cast<Base *>(derived); // checks for type compatibility
Another *dynamic_casted = dynamic_cast<Another *>(static_casted); // returns null if cast fails
Another *reinterpret_casted = reinterpret_cast<Another *>(static_casted); // does not check for type compatibility
// basically type punning
const int constant = 5;
int *const_casted = const_cast<int *>(&constant); // removes constness
}