-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.cpp
More file actions
48 lines (27 loc) · 796 Bytes
/
Copy pathcasting.cpp
File metadata and controls
48 lines (27 loc) · 796 Bytes
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
#include <iostream>
using namespace std;
int main(){
int i = 10;
double d = i;
double d1 = 10.3; // cast implicito
int i1 = d1; // trancia parte decimale
cout << i1 << endl;
// altrimwnti
double d2 = 10.3; // cast implicito
int i2 = static_cast<int>(d2); //cast statico
cout << i2 << endl;
char c = 1; //casting esplicito
int i3 = (int)c;
cout << i3 << endl;
int c1 = 1;
int *p = &c1; // OK
char c2 = 1;
// int *p = &c2; // NOO devo castarlo esplicitamente
char c3 = 1;
int *p = (int *)&c3;
cout << *p << endl; //errore
//devo utilizzzare il reinterpred cast
char c4 = 1;
int *p = reinterpret_cast<int*>(&c); // OK
return 0;
}