首页 > 其他 > 详细

通过对象成员地址求对象本身地址

时间:2015-06-10 10:19:03      阅读:165      评论:0      收藏:0      [点我收藏+]

#include "stdafx.h"
#include <iostream>
using namespace std;

class Polygon
{
public:
Polygon() {Draw();}
void Draw() {}//cout<<"Polygon::Draw()"<<endl;
void Erase() {}//cout<<"Polygon Erase()"<<endl;
~Polygon() {Erase();}
};
class Shape
{
public:
void Draw() {}//cout<<"Base::Draw()"<<endl;
void Erase() {}//cout<<"Base::Erase()"<<endl;
Shape() {Draw();} //基类构造函数,调用上面的Draw函数体
~Shape() {Erase();}//基类析构函数,调用上面的Erase函数体
public:
int a;
char b[20];
Polygon* objPolygon;
};

#define POINTER uintptr_t
#define castObject(pMenber, _group, menber) ((_group*)((unsigned int)pMenber - (unsigned int)(&((_group*)pMenber)->menber) + (unsigned int)pMenber))
#define castOBJECT(pMember, group, member) (group*)((POINTER)pMember - (POINTER)(&((group*)pMember)->member) + (POINTER)pMember)
int _tmain(int argc, _TCHAR* argv[])
{
Shape obj;
cout<<"sharp address ==="<<(unsigned int)&obj<<endl;

Polygon** pPolygon = &(obj.objPolygon);
//cout<<"sharp menber objPolygon address =="<<pPolygon<<endl;

cout<<"sharp address ==="<<(unsigned int)castObject(pPolygon, Shape, objPolygon)<<endl;
cout<<"sharp address ==="<<(unsigned int)castOBJECT(pPolygon, Shape, objPolygon)<<endl;


return 0;
}

通过对象成员地址求对象本身地址

原文:http://www.cnblogs.com/hqu-ye/p/4565207.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!