C++类型双关
Σ(っ °Д °;)っ
#include<iostream>
struct Entity
{
int x, y;
int* GetPtr()
{
return &x;
}
/*Entity* GetPtr()
{
return this;
}*/
};
int main()
{
Entity e = { 1,2 };
int* ptr = (int*)&e;
int x = *(int*)((char*)&e);
int y = *(int*)((char*)&e + 4);
std::cout << x << " " << y << " " << ptr[0] << " " << ptr[1] << std::endl;
// 1 2 1 2
ptr = e.GetPtr();
//ptr = (int*)e.GetPtr();
ptr[0] = 4;
ptr[1] = 5;
std::cout << ptr[0] << " " << ptr[1] << std::endl;
// 4 5
std::cin.get();
}