多重继承 - 构造函数的规则
来源:互联网 发布:手机淘宝天天特价女装 编辑:程序博客网 时间:2024/05/18 20:47
虚基类的子类的子类在多重继承时,构造函数初始化父类的构造函数将不会将信息传给虚基类,但可以显示调用对应虚基类的构造函数来构造自身对象,从而获得一个虚基类的对象避免多重继承的冲突;如果未显示调用虚基类的构造函数,将自动调用其默认的构造函数。
C++ primer plus 6th
14. Reusing Code in C++
New Constructor Rules
SingingWaiter(const Worker & wk, int p = 0, int v = Singer::other) : Waiter(wk,p), Singer(wk,v) {} // flawedThe problem is that automatic passing of information would pass wk to the Worker object via two separate paths (Waiter and Singer).To avoid this potential conflict, C++ disables the automatic passing of information through an intermediate class to a base class if the base class is virtual. Thus, the previous constructor will initialize the panache and voice members, but the information in the wk argument won’t get to the Waiter subobject. However, the compiler must construct a base object component before constructing derived objects;in this case, it will use the default Worker constructor.
If you want to use something other than the default constructor for a virtual base class,you need to invoke the appropriate base constructor explicitly. Thus, the constructor should look like this:
SingingWaiter(const Worker & wk, int p = 0, int v = Singer::other) : Worker(wk), Waiter(wk,p), Singer(wk,v) {}
- 多重继承 - 构造函数的规则
- 多重继承派生类的构造函数
- 多重继承类构造函数的声明
- 多重继承派生类的构造函数
- 多重继承派生类的构造函数
- 虚基类和多重继承下构造函数的顺序
- C++中多重继承构造函数调用的先后顺序
- 7.27多重继承派生类的构造函数
- 多重继承中派生类的构造函数
- 第十二周阅读程序2:多重继承的构造函数
- 派生类构造函数和多重继承的二义性问题
- 多重继承构造函数执行顺序
- 多重继承构造函数执行顺序
- 多重继承构造函数执行顺序
- 多继承中虚基类构造函数的一种调用规则
- java继承当中都有一些什么样的构造函数规则?
- C++之构造函数(三) 多重继承构造函数初始化
- 虚函数 多重继承的构造函数的调用,虚函数地址的找法
- mysql 操作小积累
- UVA540 - Team Queue
- 强烈推荐:240多个jQuery插件
- 黑马程序员之SQL学习笔记:sql 整理的一些笔记
- 游戏开发中常用的设计模式
- 多重继承 - 构造函数的规则
- 我的c 数据结构学习之旅 顺序表未完
- 实现了一些文本框的功能的窗口
- 插件__discuz x2.5插件钩子分布列表
- 天天记录 - Android抓包 - 抓取HTTP,TCP协议数据
- 文人那点事——赵千人的“艳照门”
- 内存区域划分
- equals和==的区别
- 说说char