Repository navigation
Expand file tree
/
Copy pathconvert3.cpp
More file actions
104 lines (83 loc) · 1.66 KB
/
Copy pathconvert3.cpp
File metadata and controls
104 lines (83 loc) · 1.66 KB
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
// convert3.cpp
#include <iostream>
class Integer
{
int value;
public:
Integer( int i ) : value(i)
{
}
int getValue() const
{
return value;
}
};
class SuperInt : public Integer
{
int factor;
public:
SuperInt( int i, int f ) : Integer(i), factor(f)
{
}
long getValue() const
{
return long(Integer::getValue()) * factor;
}
};
class Long
{
long value;
public:
Long( long i ) : value(i)
{
}
long getValue() const
{
return value;
}
};
int main( void )
{
{
Integer i(123);
SuperInt si(123,456);
Integer *ip = &si;
//SuperInt *sip = ip; verboten
SuperInt *sip = static_cast<SuperInt *>(ip);
// Long *l = static_cast<Long *>(&i); verboten
std::cout << sip->getValue() << std::endl;
}
{
int i = 123;
int *ip = &i;
//long *lp = ip; verboten
//long *lp = static_cast<long*>(ip); verboten
void *vp = ip;
// int *ip2 = vp; verboten
int *ip2 = static_cast<int*>(vp);
std::cout << *ip2 << std::endl;
}
{
Integer i(123);
SuperInt si(123,456);
//Long l = reinterpret_cast<Long>(i); // verboten
short s0 = 123;
short s1 = 456;
short s2 = 789;
//long l1 = reinterpret_cast<long>(s1); // verboten
long *lp = reinterpret_cast<long*>(&s1);
long l2 = reinterpret_cast<long>(&s1);
SuperInt *sp = reinterpret_cast<SuperInt *>(l2);
// short s = reinterpret_cast<short>(&s1); // verboten
short s = 123;
long *lp2 = reinterpret_cast<long*>(s);
std::cout << s0 << ' ' << *lp << ' ' << s2 << std::endl;
}
{
Integer i(123);
Integer &ir = i;
Long &lr = reinterpret_cast<Long&>( ir );
std::cout << lr.getValue() << std::endl; // undefiniertes verhalten
}
return 0;
}