C标准未指定对象布局。虚函数表(vtable)和虚基指针甚至都不是标准的一部分。因此,问题和解答只能说明可能的实现方式。
快速浏览图形似乎可以显示正确的布局。
您可能对这些进一步的参考资料感兴趣:

>关于多重继承和虚拟继承的布局的ddj文章。
>描述了vfptr(虚拟功能表,也称为vtable)和vbptr(虚拟基本Poitner)的用法。

编辑:底部继承了Right :: a或Left :: a?
在测试2中,右侧和左侧共享相同的公共父级Top。因此,在“底部”中只有一个子对象“顶部”,因此只有一个相同的Top :: a。

有趣的是,您在测试2中引入了正确的成员a。这与从Top继承的a不同。它是唯一成员,具有相同的“另一个成员”,因此,如果您通过右侧访问一个,则右侧::隐藏顶部:: a(顺便说一下,这也是底部::顶部:: a,右侧::顶部:: a,左侧::顶部:: A)。在这种情况下,bottom.a的意思是Right :: a,不是因为对象布局,而是因为名称loockup(和隐藏)规则。这不依赖于实现:它是标准且可移植的。
以下是测试2的变体,以证明这种情况:
int main() {
Bottom bottom;
bottom.a = 7;
cout << bottom.Top::a << endl << bottom.Left::Top::a << endl;
cout << bottom.Right::Top::a << endl << bottom.Left::a << endl;
cout << bottom.Right::a << endl <(&bottom) << endl;
cout << " Left: " << (void*)static_cast(&bottom) << endl;
cout << " Right: " << (void*)static_cast(&bottom) << endl;
cout << " Top::a: " << (void*)&bottom.Top::a << endl;
cout << " Left::Top::a: " << (void*)&bottom.Left::Top::a << endl;
cout << " Right::Top::a:" << (void*)&bottom.Right::Top::a << endl;
cout << " Left::a: " << (void*)&bottom.Left::a << endl;
cout << " Rigth::a: " << (void*)&bottom.Right::a << endl;
};
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-363581-1.html
唉
业界良心
全世界能单挑赢的船应该不超过10艘