标签:
#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