-
Notifications
You must be signed in to change notification settings - Fork 2
/
casting.cc
76 lines (55 loc) · 2.16 KB
/
casting.cc
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
#include <iostream>
#include <memory>
using namespace std;
struct Fruit { virtual ~Fruit(){}; };
struct Apple : public Fruit { string nameA = "apple"; };
struct Banana: public Fruit { string nameB = "banana"; };
void whatFruit(shared_ptr<Fruit> f) {
}
int main() {
// 1. Static Casting
// Used when you are taking full responsibility of type conversions and you can guarantee the conversion
// will happen correctly.
double d = 5.6;
int i = 4;
i = static_cast<int>(d);
cout << i << endl; // outputs 5, notice how floor is applied to double
// 2. Reinterpret Casting
// Used to reintrepet an object as another object.
Apple a;
Banana *b = reinterpret_cast<Banana *>(&a);
cout << b->nameB << endl; // outputs apple, notice how even if variable names are differnet, still reinterpreted
// 3. Const Cast
// Used to bypass restrictions with const and also stop const poisoning
int x = 3;
const int &y = x;
const_cast<int&>(y) = 4;
cout << y << endl; // outputs 4 now, even tho const was defined to be 3
// 4. Dynamic Cast
// Used as a safe check to see if type is convertible, if not, then throws exception
// Must have at least 1 virtual method to support dynamic cast so polymorphic. Make
// decisions based on RTTI (run time type information)
Fruit *f = new Apple;
Apple *ap = dynamic_cast<Apple *>(f);
cout << ap->nameA << endl; // Apple
//------------------ Smart Pointer Casting ---------------------
// 1. Dynamic Pointer Casting for smart pointers
shared_ptr<Fruit> ba = make_shared<Banana>();
if (dynamic_pointer_cast<Apple>(ba)) {
cout << "Apple" << endl;
} else if (dynamic_pointer_cast<Banana>(ba)) {
cout << "Banana" << endl;
} else {
cout << "Fruit" << endl;
}
// 2. Static Pointer Casting for smart pointers
shared_ptr<Fruit> ba2 = make_shared<Banana>();
if (static_pointer_cast<Banana>(ba2)) cout << "Banana" << endl;
// 3. Const pointer casting for smart poointer
shared_ptr<int> foo;
shared_ptr<const int> bar;
foo = make_shared<int>(10);
bar = const_pointer_cast<const int>(foo); // 10
cout << *bar << endl; // outputs 10
return 0;
}