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();
}
上一篇:将JPA通用的实现方法封装成一个抽象类进行拓展


下一篇:【知识图谱】08KBQA问答系统(python+fuseki+jena)