多重继承:什么是好例子? [英] Multiple Inheritance: What's a good example?

查看:203
本文介绍了多重继承:什么是好例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一个使用多重继承的好例子,这是普通接口无法做到的.

I'm trying to find a good example for the use of multiple inheritance what cannot be done with normal interfaces.

我认为很难找到无法以其他方式建模的示例.

I think it's pretty hard to find such an example which cannot be modeled in another way.

我的意思是,有人可以给我命名一个很好的真实示例,说明您何时需要使用多重继承来尽可能清晰地实现此示例.而且,它不应该使用多个接口,而应该使用可以在C ++中继承多个类的方式.

I mean, can someone name me a good real-world example of when you NEED to use multiple inheritance to implement this example as clean as possible. And it should not make use of multiple interfaces, just the way you can inherit multiple classes in C++.

推荐答案

以下是经典的方法:

class Animal {
 public:
  virtual void eat();
};

class Mammal : public Animal {
 public:
  virtual void breathe();
};

class WingedAnimal : public Animal {
 public:
  virtual void flap();
};

// A bat is a winged mammal
class Bat : public Mammal, public WingedAnimal {
};

来源: Wiki .

这篇关于多重继承:什么是好例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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