给定以下代码:
namespace Try {
class Point2d {
float _x, _y;
public:
Point2d(float x, float y) : _x(x), _y(y) {}
};
class Vertex {
Vertex* next;
public:
Vertex(Vertex* n) : next(n) {}
Vertex() {}
};
class Vertex2d : public Point2d, public Vertex {
float mumble;
public:
Vertex2d(float x, float y, float mum) : Point2d(x,y), mumble(mum) {}
};
}
int main (void)
{
using Try::Point2d;
using Try::Vertex;
using Try::Vertex2d;
Vertex2d v3d(2.5,3,4);
Vertex* pv;
Point2d* pp;
pv = &v3d;
pv = (Vertex*)(((char*)&v3d) + sizeof(Point2d));
}
有人介意解释一下为什么最后两个命令:
pv = &v3d;
pv = (Vertex*)(((char*)&v3d) + sizeof(Point2d));
(我猜这就是编译器将 pv = &v3d 翻译成..)
是完全一样的吗?我可以理解 (+sizeof(Point2d)) 在那里,因为 Vertex2d 首先是 Point2d,所以我们必须添加大小才能到达“顶点”部分。但为什么它首先将 v3d 转换为 char* ..?
谢谢