#include <string>
#include <fstream>
using namespace std;
ofstream out("reinterp.out");
class X
{
enum{sz = 5};
int a[sz];
public:
X()
{
memset(a, 0, sizeof(int) * sz);
}
virtual void f(){}
int membsize()
{
return sizeof(a);
}
friend ostream& operator<<(ostream& os, const X& x)
{
for (int i = 0; i < sz; i++)
{
os << x.a[i] << ' ';
}
return os;
}
};
int main()
{
X x;
out << x << endl;
int* xp = reinterpret_cast<int*>(&x);
xp[1] = 47;
out << x << endl;
X x2;
const int vptr_size = sizeof(X) - x2.membsize();
long l = reinterpret_cast<long>(&x2);
l += vptr_size;
xp = reinterpret_cast<int*>(l);
xp[1] = 55;
out << x2 << endl;
return 0;
}reinterpret_cast例子
最新推荐文章于 2025-08-12 21:44:24 发布
本文通过一个C++示例介绍了如何使用reinterpret_cast进行类型转换,并展示了这种转换如何影响对象内存中的数据。同时,文章还探讨了虚函数表的位置及其大小对对象内存布局的影响。
3430

被折叠的 条评论
为什么被折叠?



