class CBaseX
{
public:
int x;
CBaseX() { x = 10; }
void foo() { printf("CBaseX::foo() x=%d\n", x); }
};
class CBaseY
{
public:
int y;
int* py;
CBaseY() { y = 20; py = &y; }
void bar() { printf("CBaseY::bar() y=%d, *py=%d\n", y, *py);
}
};
class CDerived : public CBaseX, public CBaseY
{
public:
int z;
};
CDerived的内存布局(Memory Layout)
如 图所示,CDerived的内存布局包括两个对象,CBaseX 和 CBaseY,编译器也知道这一点。因此,当你将CDerived* 转换到 CBaseY*时,它给指针添加4个字节,同时当你将CBaseY*转换到CDerived*时,它给指针减去4。然而,甚至它即便不是一个 CDerived你也可以这样做。
转自:http://hi.baidu.com/elseif/blog/item/3abd0216b61fd14b20a4e9e2.html