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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include <iostream>
#include <cstdint>
struct Point
{
int x, y;
Point(int x, int y) : x(x), y(y) {}
};
struct Color
{
uint8_t r, g, b, a;
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) : r(r), g(g), b(b), a(a) {}
};
void ReinterpretCastDemo()
{
std::cout << "=== reinterpret_cast Demo ===" << std::endl;
// 1. 指针类型之间的转换
Point point(10, 20);
int* intPtr = reinterpret_cast<int*>(&point);
std::cout << "Point: (" << point.x << ", " << point.y << ")" << std::endl;
std::cout << "As int array: [" << intPtr[0] << ", " << intPtr[1] << "]" << std::endl;
// 修改通过int指针
intPtr[0] = 100;
intPtr[1] = 200;
std::cout << "Modified Point: (" << point.x << ", " << point.y << ")" << std::endl;
// 2. 整数和指针之间的转换
uintptr_t address = reinterpret_cast<uintptr_t>(&point);
std::cout << "Point address as integer: 0x" << std::hex << address << std::dec << std::endl;
Point* pointPtr = reinterpret_cast<Point*>(address);
std::cout << "Back to pointer: (" << pointPtr->x << ", " << pointPtr->y << ")" << std::endl;
// 3. 不同结构体之间的转换(危险)
Color color(255, 128, 64, 255);
uint32_t* colorAsInt = reinterpret_cast<uint32_t*>(&color);
std::cout << "Color RGBA: (" << (int)color.r << ", " << (int)color.g
<< ", " << (int)color.b << ", " << (int)color.a << ")" << std::endl;
std::cout << "Color as uint32: 0x" << std::hex << *colorAsInt << std::dec << std::endl;
// 4. 函数指针转换
void (*funcPtr)() = reinterpret_cast<void(*)()>(0x12345678);
std::cout << "Function pointer: " << reinterpret_cast<void*>(funcPtr) << std::endl;
// 5. 字节级别的数据访问
double pi = 3.14159265359;
uint64_t* piAsInt = reinterpret_cast<uint64_t*>(&pi);
std::cout << "Pi: " << pi << std::endl;
std::cout << "Pi as uint64: 0x" << std::hex << *piAsInt << std::dec << std::endl;
// 分析IEEE 754格式
uint8_t* bytes = reinterpret_cast<uint8_t*>(&pi);
std::cout << "Pi bytes: ";
for (int i = 0; i < 8; ++i)
{
std::cout << "0x" << std::hex << (int)bytes[i] << " ";
}
std::cout << std::dec << std::endl;
}
// 实际应用:序列化
class Serializer
{
public:
template<typename T>
static void SerializePOD(const T& obj, uint8_t* buffer)
{
static_assert(std::is_trivially_copyable_v<T>, "Type must be trivially copyable");
const uint8_t* objBytes = reinterpret_cast<const uint8_t*>(&obj);
for (size_t i = 0; i < sizeof(T); ++i)
{
buffer[i] = objBytes[i];
}
}
template<typename T>
static T DeserializePOD(const uint8_t* buffer)
{
static_assert(std::is_trivially_copyable_v<T>, "Type must be trivially copyable");
T obj;
uint8_t* objBytes = reinterpret_cast<uint8_t*>(&obj);
for (size_t i = 0; i < sizeof(T); ++i)
{
objBytes[i] = buffer[i];
}
return obj;
}
};
void SerializationDemo()
{
std::cout << "\n=== Serialization Demo ===" << std::endl;
Point originalPoint(42, 84);
uint8_t buffer[sizeof(Point)];
// 序列化
Serializer::SerializePOD(originalPoint, buffer);
std::cout << "Original point: (" << originalPoint.x << ", " << originalPoint.y << ")" << std::endl;
std::cout << "Serialized bytes: ";
for (size_t i = 0; i < sizeof(Point); ++i)
{
std::cout << "0x" << std::hex << (int)buffer[i] << " ";
}
std::cout << std::dec << std::endl;
// 反序列化
Point deserializedPoint = Serializer::DeserializePOD<Point>(buffer);
std::cout << "Deserialized point: (" << deserializedPoint.x << ", " << deserializedPoint.y << ")" << std::endl;
}
int main()
{
ReinterpretCastDemo();
SerializationDemo();
return 0;
}
|