虚拟继承如何解决“钻石”问题(多重继承)歧义?
class A { public: void eat(){ cout<<"A";} };
class B: virtual public A { public: void eat(){ cout<<"B";} };
class C: virtual public A { public: void eat(){ cout<<"C";} };
class D: public B,C { public: void eat(){ cout<<"D";} };
int main(){
A *a = new D();
a->eat();
}
我理解钻石问题,上面的代码没有这个问题。
虚拟继承到底是如何解决问题的呢?
我的理解: 当我说 A *a = new D();
时,编译器想知道 D
类型的对象是否可以分配给 A 类型的指针
,但它有两条可以遵循的路径,但不能自行决定。
那么,虚拟继承如何解决这个问题(帮助编译器做出决定)?
class A { public: void eat(){ cout<<"A";} };
class B: virtual public A { public: void eat(){ cout<<"B";} };
class C: virtual public A { public: void eat(){ cout<<"C";} };
class D: public B,C { public: void eat(){ cout<<"D";} };
int main(){
A *a = new D();
a->eat();
}
I understand the diamond problem, and above piece of code does not have that problem.
How exactly does virtual inheritance solve the problem?
What I understand:
When I say A *a = new D();
, the compiler wants to know if an object of type D
can be assigned to a pointer of type A
, but it has two paths that it can follow, but cannot decide by itself.
So, how does virtual inheritance resolve the issue (help compiler take the decision)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你想要:(可以通过虚拟继承实现)
而不是:(没有虚拟继承会发生什么)
虚拟继承意味着只有 1 个基
A 实例
类不是 2。您的类型
D
将有 2 个 vtable 指针(您可以在第一个图中看到它们),一个用于B
,一个用于C
实际上继承了A
。D
的对象大小增加了,因为它现在存储了 2 个指针;然而现在只有一个A
。因此,
B::A
和C::A
是相同的,因此来自D
的调用不会有歧义。如果您不使用虚拟继承,您将看到上面的第二张图。然后,对 A 成员的任何调用都会变得不明确,您需要指定要采用的路径。维基百科在这里还有另一个很好的概述和示例
You want: (Achievable with virtual inheritance)
And not: (What happens without virtual inheritance)
Virtual inheritance means that there will be only 1 instance of the base
A
class not 2.Your type
D
would have 2 vtable pointers (you can see them in the first diagram), one forB
and one forC
who virtually inheritA
.D
's object size is increased because it stores 2 pointers now; however there is only oneA
now.So
B::A
andC::A
are the same and so there can be no ambiguous calls fromD
. If you don't use virtual inheritance you have the second diagram above. And any call to a member of A then becomes ambiguous and you need to specify which path you want to take.Wikipedia has another good rundown and example here
为什么还有另一个答案?
好吧,SO 上的许多帖子和外部文章都说,钻石问题是通过创建
A
的单个实例而不是两个(D
的每个父级一个)来解决的,从而解决了歧义。然而,这并没有让我对流程有全面的了解,我最终遇到了更多问题,比如B
和C
尝试创建A 的不同实例会怎样?
例如,使用不同的参数调用参数化构造函数(D::D(int x, int y): C(x), B(y) {}
)?A
的哪个实例将被选择成为D
的一部分?B
使用非虚拟继承,但对C
使用虚拟继承,会怎么样?在D
中创建A
的单个实例是否足够?如果不尝试代码示例就无法预测行为意味着不理解这个概念。下面是帮助我了解虚拟继承的内容。
Double A
首先,让我们从这段没有虚拟继承的代码开始:
让我们看一下输出。执行
B b(2);
按预期创建A(2)
,与C c(3);
相同:D d (2, 3);
需要B
和C
,它们各自创建自己的A
,所以我们有两个 <d
中的code>A:这就是
d.getX()
导致编译错误的原因,因为编译器无法选择哪个A 它应该调用方法的实例。仍然可以直接调用所选父类的方法:
虚拟性
现在让我们添加虚拟继承。使用相同的代码示例并进行以下更改:
让我们跳转到
d
的创建:您可以看到,
A
是使用默认构造函数创建的,忽略从B 构造函数传递的参数
和C
。由于歧义性消失,对getX()
的所有调用都会返回相同的值:但是如果我们想为
A
调用参数化构造函数该怎么办?可以通过从D
的构造函数显式调用它来完成:通常,类只能显式使用直接父级的构造函数,但虚拟继承情况除外。发现这条规则对我来说“很有意义”,并且对理解虚拟接口有很大帮助:
代码
class B: virtual A
意味着,任何从B
继承的类现在都负责用于自行创建A
,因为B
不会自动执行此操作。记住此声明后,很容易回答我遇到的所有问题
D
的创建B
和C
都不负责A
的参数,完全取决于D<仅/代码>。
C
会将A
的创建委托给D
,但B
将创建自己的A< 实例/code> 从而使钻石问题重新出现
Why another answer?
Well, many posts on SO and articles outside say, that diamond problem is solved by creating single instance of
A
instead of two (one for each parent ofD
), thus resolving ambiguity. However, this didn't give me comprehensive understanding of process, I ended up with even more questions likeB
andC
tries to create different instances ofA
e.g. calling parametrized constructor with different parameters (D::D(int x, int y): C(x), B(y) {}
)? Which instance ofA
will be chosen to become part ofD
?B
, but virtual one forC
? Is it enough for creating single instance ofA
inD
?Not being able to predict behavior without trying code samples means not understanding the concept. Below is what helped me to wrap head around virtual inheritance.
Double A
First, lets start with this code without virtual inheritance:
Lets go through output. Executing
B b(2);
createsA(2)
as expected, same forC c(3);
:D d(2, 3);
needs bothB
andC
, each of them creating its ownA
, so we have doubleA
ind
:That's the reason for
d.getX()
to cause compilation error as compiler can't choose whichA
instance it should call method for. Still it's possible to call methods directly for chosen parent class:Virtuality
Now lets add virtual inheritance. Using same code sample with the following changes:
Lets jump to creation of
d
:You can see,
A
is created with default constructor ignoring parameters passed from constructors ofB
andC
. As ambiguity is gone, all calls togetX()
return the same value:But what if we want to call parametrized constructor for
A
? It can be done by explicitly calling it from constructor ofD
:Normally, class can explicitly use constructors of direct parents only, but there is an exclusion for virtual inheritance case. Discovering this rule "clicked" for me and helped understanding virtual interfaces a lot:
Code
class B: virtual A
means, that any class inherited fromB
is now responsible for creatingA
by itself, sinceB
isn't going to do it automatically.With this statement in mind it's easy to answer all questions I had:
D
creation neitherB
norC
is responsible for parameters ofA
, it's totally up toD
only.C
will delegate creation ofA
toD
, butB
will create its own instance ofA
thus bringing diamond problem back派生类的实例存储其基类的成员。
没有虚拟继承,内存布局看起来像(注意
D
类中A
成员的两个副本) :使用虚拟继承,内存布局看起来像(注意
D
类中A
成员的单个副本):对于每个派生类,编译器都会创建一个虚拟表,其中保存指向派生类中存储的虚拟基类成员的指针,并在派生类中添加一个指向该虚拟表的指针。
Instances of derived classes store the members of their base classes.
Without virtual inheritance, the memory layouts look like (note the two copies of the
A
members in classD
):With virtual inheritance, the memory layouts look like (note the single copy of the
A
members in classD
):For each derived class, the compiler creates a virtual table holding pointers to the members of its virtual base classes stored in the derived class, and adds a pointer to that virtual table in the derived class.
问题不在于编译器必须遵循的路径。问题在于该路径的端点:转换的结果。当涉及到类型转换时,路径并不重要,重要的是最终结果。
如果使用普通继承,每个路径都有自己独特的端点,这意味着强制转换的结果是不明确的,这就是问题所在。
如果使用虚拟继承,则会得到菱形层次结构:两条路径都通向同一端点。在这种情况下,选择路径的问题不再存在(或者更准确地说,不再重要),因为两条路径都会导致相同的结果。结果不再含糊不清——这才是重要的。确切的路径没有。
The problem is not the path the compiler must follow. The problem is the endpoint of that path: the result of the cast. When it comes to type conversions, the path does not matter, only the final result does.
If you use ordinary inheritance, each path has its own distinctive endpoint, meaning that the result of the cast is ambiguous, which is the problem.
If you use virtual inheritance, you get a diamond-shaped hierarchy: both paths leads to the same endpoint. In this case the problem of choosing the path no longer exists (or, more precisely, no longer matters), because both paths lead to the same result. The result is no longer ambiguous - that is what matters. The exact path doesn't.
实际上,示例应该如下所示:
... 这样输出将是正确的:“EAT=>D”
虚拟继承仅解决了祖父的重复问题!
但您仍然需要将方法指定为虚拟,以便正确覆盖这些方法......
Actually the example should be as follows:
... that way the output is gonna be the correct one: "EAT=>D"
Virtual inheritance only solves the duplication of the grandfather!
BUT you still need to specify the methods to be virtual in order to get the methods correctly overrided...