-
Notifications
You must be signed in to change notification settings - Fork 160
/
typecast.cpp
89 lines (71 loc) · 2.18 KB
/
typecast.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
/*
# typecast
Bibliography:
- http://www.cplusplus.com/doc/tutorial/typecasting/
- http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast
- http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used
*/
#include "common.hpp"
int main() {
/*
Implicit typecast.
Works if types are convertible, either by the compiler or by a constructor that takes one single argument.
*/
{
//convertible basetypes
//same as in C
{
int i = 0.25;
assert(i == 0);
}
// Via constructor that takes a single argument and is not explicit
// works becaues the constructor `C(int)` exists.
{
class C {
public:
int i;
C(int i) : i(i) {}
};
C c = 1;
assert(c.i == 1);
}
}
/*
# explicit typecast
*/
{
/*
# c style typecast
TODO what does it do in relation to the others? why should it not be used?
*/
/*
# dynamic_cast
- done at runtime
- only for pointers or references
- can only be done from / to base derived
- always works for derived to base
- baes to derived:
- compiles only for polymorphic classes
- a runtime check is done to see if the cast is correct or an exception is thrown
*/
{
}
/*
# reinterpret_cast
Converts anything to anything by copying bytes.
Behaviour is not portable in general.
*/
// Function pointer typecast.
{
// Required when a function is overloaded, and you want to use a function pointer.
// TODO understand syntax.
{
std::string s = "a,bc. d";
auto end = s.end();
s.erase(std::remove_if(s.begin(), end, (int(*)(int))std::ispunct), end);
std::cout << s << std::endl;
assert(s == "abc d");
}
}
}
}