C ++:具有多态性的多重继承 [英] C++ : Multiple inheritance with polymorphism

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

问题描述

(请原谅菜鸟问题)

我有4个课程:

class Person {};
class Student : public Person {};
class Employee : public Person {};
class StudentEmployee : public Student, public Employee {};

基本上,Person是基类,直接由StudentEmployee子类化. StudentEmployee使用多重继承来同时继承StudentEmployee的子类.

Essentially Person is the base class, which are directly subclassed by both Student and Employee. StudentEmployee employs multiple inheritance to subclass both Student and Employee.

Person pat = Person("Pat");
Student sam = Student("Sam");
Employee em = Employee("Emily");
StudentEmployee sen = StudentEmployee("Sienna");


Person ppl[3] = {pat, sam, em};
//compile time error: ambiguous base class
//Person ppl[4] = {pat, sam, em, sen}; 

当使用基类Person的数组时,可以将Person及其所有子类放入此数组中.给出StudentEmployee除外,原因是基类模棱两可.

When I use an array of Person, the base class, I can put Person and all of its subclasses inside this array. Except for StudentEmployee, given the reason ambiguous base class.

鉴于StudentEmployee被保证具有Person的所有方法和属性,StudentEmployee是否被视为Person的子类?

Given that StudentEmployee is guaranteed to have all the methods and attributes of Person, is StudentEmployee considered a subclass of Person?

  • 如果是这样,为什么编译器不允许我将对象分配给其超类类型的变量?
  • 如果没有,为什么不呢?以及完成此任务的正确方法是什么?

欢呼

先决条件,这个问题与以下任一问题均不相同:
多态与继承有关
继承在C ++中会破坏多态吗?

Preemptively, this question is NOT the same as either of the following:
polymorphism relates inheritance
Inheritance mucking up polymorphism in C++?

推荐答案

StudentEmployee当然是Person的子类.问题是两次:它间接继承了Person两次(一次通过Student一次,一次通过Employee),这就是为什么您会收到模棱两可的基类"错误的原因.为了确保StudentEmployee仅继承一次Person,您必须使用虚拟继承,如下所示:

StudentEmployee certainly is a subclass of Person. The problem is it is so twice: It indirectly inherits Person twice (once through Student and once through Employee) and that's why you get the "ambiguous base class" error. To make sure StudentEmployee only inherits Person once, you have to use virtual inheritance, like so:

class Person {};
class Student : public virtual Person {};
class Employee : public virtual Person {};
class StudentEmployee : public Student, public Employee {};

这将解决您的错误.

不过,您的代码还有另一个大问题,它叫做

There is another big problem with your code, though, and it's called slicing.

执行此操作时:

Person ppl[3] = {pat, sam, em};

将创建三个Person对象的数组,但是将使用Person类的隐式定义的复制构造函数对这些对象进行复制构造.现在,问题在于数组中的对象只是Person对象,而不是您希望它们成为子类的对象.

An array of three Person objects will be created but those objects will be copy constructed using the implicitly defined copy constructor of the Person class. Now, the problem with this is that the objects in your array will be just Person objects and not objects of the subclasses you want them to be.

要解决此问题,您必须创建一个指向Person对象的指针数组,如下所示:

To fix this, you'll have to make an array of pointers to Person objects, like this:

Person* ppl[] = {new Person("Pat"), new Student("Sam"),
                 new Employee("Emily"), new StudentEmployee("Sienna")};

Person* ppl[] = {&pat, &sam, &em, &sen};

这篇关于C ++:具有多态性的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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