多重继承,C ++和多重超类中的相同方法签名 [英] Multiple Inheritance, C++ and Same Method Signature in Multiple Super Classes

查看:110
本文介绍了多重继承,C ++和多重超类中的相同方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有C ++的经验,我来自Java背景。最近,我在接受采访时询问为什么Java不允许多重继承,答案很容易。但是,我仍然对C ++如何处理它,因为它允许你继承多个类。

I have no experience in C++, and I come from a Java background. Lately, I was asked in an interview on why Java would not allow multiple inheritence and the answer was pretty easy. However, I am still curious on how C++ deals with that since it allows you to inherit from more than one class.

具体来说,有一个类 MechanicalEngineer ,另一个名为 ElectricalEngineer 。两者都有一个名为 buildRobot()的方法。

Specifically, say there is a class called MechanicalEngineer and another called ElectricalEngineer. Both have a method called buildRobot().

如果我们创建第三个类 RoboticsEngineer ,这两者都有,并且不重写该方法,你只需调用:

What happens if we make a third class RoboticsEngineer, that inherets from both and does not override that method, and you just call:

(some instance of RoboticsEngineer).buildRobot()

会抛出异常,的超类将被使用?如果是这样,编译器如何知道使用哪个类?

Will an exception be thrown, or the method from one of the super-classes will be used? If so, how does the compiler know which class to use?

推荐答案

编译器会标记这种情况调用(一些RoboticsEngineer实例).buildRobot())作为错误。

The compiler will flag this kind of situation (i.e., attempting to call (some instance of RoboticsEngineer).buildRobot()) as an error.

对象已经拥有了它自身内部的两个基础对象( MechanicalEngineer 实例和 ElectricalEngineer 实例)的副本,只有签名不足以告诉使用哪一个。

This happens because the derived object has got a copy of both base objects (a MechanicalEngineer instance and an ElectricalEngineer instance) inside of itself and the method signature alone is not enough to tell which one to use.

如果在<$ c $中覆盖 buildRobot c> RoboticsEngineer ,您将能够通过前缀类名来明确说明要使用哪个继承方法,例如:

If you override buildRobot in your RoboticsEngineer, you will be able to say explicitly which inherited method to use by prefixing the class name, e.g.:

void RoboticsEngineer::buildRobot() {
    ElectricalEngineer::buildRobot()
}

通过同一个硬币,你可以实际上强制编译器使用 buildRobot 的一个版本或者另一个前缀类名: / p>

By the same coin, you can actually "force" the compiler to use one version or another of buildRobot by prefixing it with the class name:

 (some instance of RoboticsEngineer).ElectricalEngineer::buildRobot();

在此情况下, ElectricalEngineer 方法将被调用,没有歧义。

in this case the ElectricalEngineer implementation of the method will be called, no ambiguity.

当你有一个 Engineer MechanicalEngineer ElectricalEngineer ,并指定继承为 virtual 在这两种情况下。当使用 virtual 时,派生对象不包含 Engineer 的两个实例,但编译器确保只有一个。这将是这样:

A special case is given when you have an Engineer base class to both MechanicalEngineer and ElectricalEngineer and you specify the inheritance to be virtual in both cases. When virtual is used, the derived object does not contain two instances of Engineer, but the compiler makes sure that there is only one of it. This would look like this:

 class Engineer {
      void buildRobot();
 };

 class MechanicalEngineer: public virtual Engineer {

 };

 class ElectricalEngineer: public virtual Engineer {

 };

在这种情况下,

(some instance of RoboticsEngineer).buildRobot();

将无歧义地解决。如果buildRobot声明为 virtual ,并且在两个派生类之一中重写,同样如此。无论如何,如果两个派生类(ElectricalEngineer和MechanicalEngineer)覆盖 buildRobot ,则再次出现歧义,编译器将标记调用的尝试of RoboticsEngineer).buildRobot(); 作为错误。

will be resolved without ambiguities. The same is true if buildRobot is declared virtual and overridden in one of the two derived classes. Anyway, if both derived classes (ElectricalEngineer and MechanicalEngineer) overrides buildRobot, then ambiguity arises once again and the compiler will flag the attempt at calling (some instance of RoboticsEngineer).buildRobot(); as an error.

这篇关于多重继承,C ++和多重超类中的相同方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆