多重继承问题的基指针偏移调整
我知道在这种情况下会发生基本偏移调整
class Mother {
public:
virtual void MotherMethod() {}
int mother_data;
};
class Father {
public:
virtual void FatherMethod() {}
int father_data;
};
class Child : public Mother, public Father {
public:
virtual void ChildMethod() {}
int child_data;
};
Father *f = new Child;
在编译期间,这段代码相当于
Child *tmp = new Child;
Father *f = tmp ? tmp + sizeof(Mother) : 0;
我的问题是这个偏移量在编译阶段是如何确定的? 例如,在下面的情况下,
void fun(Father* f) {
// do something
}
我们不知道指针将接收什么对象,如何确定编译过程中是否需要调整偏移量或需要调整多少偏移量。
希望有人能解答我的疑惑,非常感谢!
I know base offset adjustment will happen in this situation
class Mother {
public:
virtual void MotherMethod() {}
int mother_data;
};
class Father {
public:
virtual void FatherMethod() {}
int father_data;
};
class Child : public Mother, public Father {
public:
virtual void ChildMethod() {}
int child_data;
};
Father *f = new Child;
During compilation, this code is equivalent to
Child *tmp = new Child;
Father *f = tmp ? tmp + sizeof(Mother) : 0;
My question is how this offset is determined in the compilation phase?
for example, in the following case
void fun(Father* f) {
// do something
}
we don't know what object the pointer will receive, how is it possible to determine whether or how much offset adjustment is needed during compilation.
I hope someone can answer my doubts, Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数的调用者确切地知道涉及哪些类型并进行必要的调整。
也就是说,
行为完全相同
,但转换是隐式的。
The caller of a function knows exactly what types are involved and does the necessary adjustments.
That is,
behaves exactly the same as
but the conversion is implicit.