reinterpret_cast例子

来源:互联网 发布:淘宝网官能卖保健品 编辑:程序博客网 时间:2024/06/06 03:56
#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;}

0 0
原创粉丝点击