Cpp多重继承会产生的问题
多重继承常常被认为是 OOP 中一种复杂且不必要的部分。多重继承面临 crash 的场景并非难以想象,来看下面的例子。
1. 名称冲突
来看以下情况:
如果 Dog 类以及 Bird 类都有一个名为 eat() 的方法,而子类又没有 override 该方法。如果此时调用子类的 eat() 方法,编译器就会报错,指出 eat() 的调用有歧义(不知道是调用从 Dog 类继承而来的 eat() 方法,还是从 Bird 类继承而来的 eat() 方法)。代码如下:
class Dog
{
public:
virtual void eat() {};
}; class Bird
{
public:
virtual void eat() {};
}; class DogBird : public Dog, public Bird
{ }; int main()
{
DogBird db;
db.eat(); // BUG! Ambiguous call to method eat()
return 0;
} /*
编译错误:
[purple@archlinux AAA]$ clang++ aa.cc
aa.cc:21:8: error: member 'eat' found in multiple base classes of different types
db.eat(); // BUG! Ambiguous call to method eat()
^
aa.cc:4:18: note: member found by ambiguous name lookup
virtual void eat() {};
^
aa.cc:10:18: note: member found by ambiguous name lookup
virtual void eat() {};
^
1 error generated.
*/
解决方法:
#include <iostream>
using namespace std; class Dog
{
public:
virtual void eat() {cout << "The Dog has eaten." << endl;};
}; class Bird
{
public:
virtual void eat() {cout << "The Bird has eaten." << endl;};
}; class DogBird : public Dog, public Bird
{ }; int main()
{
DogBird db;
static_cast<Dog>(db).eat(); // Slices, calling Dog::eat()
db.Bird::eat(); // Calls Bird::eat()
return 0;
} /*
Output:
The Dog has eaten.
The Bird has eaten.
*/
为了消除歧义,要么在 DogBird类重写 eat() 方法,要么显示的指明调用的是哪一个父类的版本。
2. 歧义基类
来看以下情况:
虽然可能产生 name ambiguity,但是 C++ 允许这种类型的类层次结构。例如,如果 Animal 类有一个 public 方法 sleep(),那么 DogBird 对象将无法调用这个方法,因为编译器不知道调用 Dog 继承的版本还是 Bird 继承的版本。代码如下:
class Animal
{
public:
void sleep(){}
}; class Dog : public Animal
{
}; class Bird : public Animal
{
}; class DogBird : public Dog, public Bird
{
}; int main()
{
DogBird db;
db.sleep();
return 0;
} /*
发生编译错误 [purple@archlinux ~]$ clang++ aa.cc
aa.cc:25:8: error: non-static member 'sleep' found in multiple base-class subobjects of type 'Animal':
class DogBird -> class Dog -> class Animal
class DogBird -> class Bird -> class Animal
db.sleep();
^
aa.cc:7:10: note: member found by ambiguous name lookup
void sleep(){}
^
1 error generated.
*/
使用“菱形”类层次结构的最佳方法是将最顶部的类设置为抽象类,所有方法都设置为纯虚方法。由于类只声明方法而不提供定义,在基类中没有方法可以调用,因此在这个层次上就不会产生歧义。代码如下:
#include <iostream>
using namespace std; class Animal
{
public:
virtual void sleep() = 0;
}; class Dog : public Animal
{
public:
virtual void sleep()
{
cout << "Dog sleep!" << endl;
}
}; class Bird : public Animal
{
public:
virtual void sleep()
{
cout << "Bird sleep!" << endl;
}
}; class DogBird : public Dog, public Bird
{
public:
// 注意:虽然从语法上来说,DogBird可以不override sleep方法
// 但是如此一来,再调用DogBird类的sleep方法时,会分不清是Dog类的还是Bird类的
virtual void sleep()
{
cout << "DogBird sleep!" << endl;
}
}; int main()
{
DogBird db;
db.sleep();
return 0;
} /*
Output:
DogBird sleep!
*/
小结
我们往往会在定义一个“既是一个事物同时又是另外一个事物”的情况下使用多重继承,然而,实际上遵循这个模式的实际对象很难恰如其分的转换为合适的代码,因此在工程中,我们要尽量避免使用多重继承。
Cpp多重继承会产生的问题的更多相关文章
- 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类
[源码下载] 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 多重继承 虚基类 示例1 ...
- cl.exe命令方式编译cpp
直接在命令行窗口调用cl编译cpp文件 往往不能通过. 主要原因是一些头文件及可执行文件未在环境变量中设置.可以通过执行VSVAR32.BAT批处理文件来设置环境变量,注意vs2005跟2008的放置 ...
- C++-多重继承的注意点
1, 钻石型多重继承如果不想要底部的类有重复的变量,则需要声明为virtual继承 class File{...}; class InputFile: virtual public File{..}; ...
- C++中的多重继承与虚继承的问题
1.C++支持多重继承,但是一般情况下,建议使用单一继承. 类D继承自B类和C类,而B类和C类都继承自类A,因此出现下图所示情况: A A \ / B C ...
- C++多重继承虚表的内存分布
接前面虚表的内存分布,今天重点看多重继承的虚表内存分布,简单的说,继承几个类便有几个虚表,如下代码 class Drive : public Base1, public Base2, public B ...
- Linux Debugging(四): 使用GDB来理解C++ 对象的内存布局(多重继承,虚继承)
前一段时间再次拜读<Inside the C++ Object Model> 深入探索C++对象模型,有了进一步的理解,因此我也写了四篇博文算是读书笔记: Program Transfor ...
- 【ThinkingInC++】75、多重继承
第九章 多重继承 9.2 接口继承 Intertfacees.cpp /** * 书本:[ThinkingInC++] * 功能:接口继承Interfaces.cpp * 时间:2014年10月28日 ...
- 《C++ Primer Plus》14.3 多重继承 学习笔记
多重继承(MI)描述的是有多个直接基类的类.与单继承一样,共有MI表示的也是is-a关系.例如,可以从Awiter类和Singer类派生出SingingWaiter类:class SingingWai ...
- C++解析(24):抽象类和接口、多重继承
0.目录 1.抽象类和接口 1.1 抽象类 1.2 纯虚函数 1.3 接口 2.被遗弃的多重继承 2.1 C++中的多重继承 2.2 多重继承的问题一 2.3 多重继承的问题二 2.4 多重继承的问题 ...
随机推荐
- mac下的ssh自动登陆
终端的ssh是标准的OpenSSH client 如果需要克隆会话功能,可以通过配置打开. $ cat .ssh/config Host * ControlMaster auto ControlPat ...
- sp.ui.rte.js参数错误
出现这个错误的原因是,在新建页面我用spd隐藏了两行不需要的填写的控件. 去掉这两个隐藏属性就可以了.
- Modelsim的demo入门教程
写在前面的话学过MCU设计的朋友都知道,系统调试是多么的重要.而对于FPGA设计来说,仿真确实最重要的.一个完整的项目,必须有完整的仿真平台.有朋友说,按键仿真模型没法搞. 我只能说,你并不了解硬件及 ...
- js 获取随机数
返回 m 到 n 的随机整数 <script type="text/javascript"> function randomNumber(m.n){ return Ma ...
- Integer对象
数字格式的字符串转成基本数据类型的方法: 1:将该字符串封装成了Integer对象,并调用对象的方法intValue(); 2:使用Integer.parseInt(numstring):不用建立对象 ...
- 【Masonry】使用技巧 - 篇一
从别人项目得到的灵感 : 请看以下代码 UIColor *darkColor = [UIColor colorWithHexString:@"0x28303b"]; // 1. 确 ...
- unity工具IGamesTools之批量生成帧动画
unity工具IGamesTools批量生成帧动画,可批量的将指定文件夹下的帧动画图片自动生成对应的资源文件(Animation,AnimationController,Prefabs) unity工 ...
- 微软职位内部推荐-Software Engineer II-News
微软近期Open的职位: News is a critical areas for integration of mobile and services, one of the top priorit ...
- 字符串流sstream[part1/基本知识]
C++中的输入输出分为三种:基于控制台的I/O,即istream.ostream.iostream:基于文件的I/O,即ifstream.ofstream.fstream:基于字符串的I/O,即ist ...
- Daily Scrum6
今天我们小组开会内容分为以下部分: part 1: Anti-spam and anti-abuse module模块总结: part 2: 部分优化代码的展示于交流: part 3:针对用户积分模块 ...