admin 管理员组

文章数量: 887007

钻石型多重继承

钻石型多重继承

by Onur Tuna

通过Onur Tuna

C ++中的多重继承与钻石问题 (Multiple Inheritance in C++ and the Diamond Problem)

Unlike many other object-oriented programming languages, C++ allows multiple inheritance.

与许多其他面向对象的编程语言不同,C ++允许多重继承。

Multiple inheritance allows a child class to inherit from more than one parent class.

多重继承允许子类继承多个父类。

At the outset, it seems like a very useful feature. But a user needs to be mindful of a few gotchas while implementing this feature.

一开始,它似乎是一个非常有用的功能。 但是用户在实现此功能时需要注意一些陷阱

In the examples below, we will cover a few scenarios that one needs to be mindful about.

在下面的示例中,我们将介绍一些需要注意的场景。

We’ll start with a simple example to explain this concept in C++.

我们将从一个简单的示例开始,用C ++解释这个概念。

The output of this code is as follows:

此代码的输出如下:

I'm breathing as a snake.I’m crawling as a snake.

In the example above, we have a base class named as LivingThing. The Animal and Reptile classes inherit from it. Only the Animal class overrides the method breathe(). The Snake class inherits from the Animal and Reptile classes. It overrides their methods. In the example above, there is no problem. Our code works well.

在上面的示例中,我们有一个名为LivingThing的基类。 动物 Reptile类从中继承。 只有动物 类重写方法breathe()Snake类继承自AnimalReptile类。 它覆盖了他们的方法。 在上面的示例中,没有问题。 我们的代码运行良好。

Now, we’ll add a bit of complexity.

现在,我们将增加一些复杂性。

What if Reptile class overrides the breathe() method?

如果Reptile类重写breathe()方法怎么办?

The Snake class would not know which breathe() method to call. This is the “Diamond Problem”.

Snake类不知道要调用哪个breathe()方法。 这就是“钻石问题”。

钻石问题 (Diamond Problem)

Look at the code below. It is like the code in the example above, except that we have overridden the breathe() method in the Reptile class.

看下面的代码。 就像上面示例中的代码一样,除了我们在Reptile类中重写了breathe()方法。

If you try compiling the program, it won’t. You’ll be staring at an error message like the one below.

如果您尝试编译程序,则不会。 您将盯着下面的错误消息。

member ‘breathe’ found in multiple base classes of different types

The error is due to the “Diamond Problem” of multiple inheritance. The Snake class does not know which breathe() method to call.

该错误归因于多重继承的“钻石问题”。 Snake类不知道要调用哪个breathe()方法。

In the first example, only the Animal class had overridden the breathe() method. The Reptile class had not. Hence, it wasn’t very difficult for the Snake class to figure out which breathe() method to call. And the Snake class ended up calling the breathe() method of the Animal class.

在第一个示例中,只有Animal类覆盖了breathe() 方法。 爬虫类没有。 因此,对于Snake类来说,找出要调用的breathe()方法并不困难。 Snake类最终调用了Animal类的breathe()方法。

In the second example, the Snake class inherits two breathe() methods. The breathe() method of the Animal and Reptile class. Since we haven’t overridden the breathe() method in the Snake class, there is ambiguity.

在第二个示例中,Snake类继承了两个 breathe()方法。 AnimalReptile类的breathe()方法。 由于我们没有在Snake类中重写breathe()方法,因此存在歧义。

C++ has many powerful features such as multiple inheritance. But, it is not necessary that we use all the features it provides.

C ++具有许多强大的功能,例如多重继承。 但是,我们不必使用它提供的所有功能。

I don’t prefer using multiple inheritance and use virtual inheritance instead.

我不喜欢使用多重继承,而使用虚拟继承。

Virtual inheritance solves the classic “Diamond Problem”. It ensures that the child class gets only a single instance of the common base class.

虚拟继承解决了经典的“钻石问题”。 它确保子类仅获得公共基类的单个实例。

In other words, the Snake class will have only one instance of the LivingThing class. The Animal and Reptile classes share this instance.

换句话说, Snake类将仅具有LivingThing类的一个实例。 AnimalReptile类共享此实例。

This solves the compile time error we receive earlier. Classes derived from abstract classes must override the pure virtual functions defined in the base class.

这解决了我们之前收到的编译时错误。 从抽象类派生的类必须重写基类中定义的纯虚函数。

I hope you enjoyed this overview of multiple inheritance and the “Diamond Problem”.

我希望您喜欢多重继承和“钻石问题”的概述。

翻译自: /

钻石型多重继承

本文标签: 钻石型多重继承