c++11的构造函数继承
https://en.cppreference.com/w/cpp/language/using_declaration
在[Inheriting constructors]这一节.
其实叫做"基类的构造函数前置"比较好.
像mystring继承自string类,但仍然是提供字符串功能.new的时候仍旧希望保留旧有的初始化传参方式.这时候在mystring里一一再实现(调用基类)就比较麻烦.
但在c++11之前只能这样.
c++11之前的默认构造方式(淡然c++11之后还是)是,先把各个基类的默认初始化函数调用一遍.然后对派生字段做默认初始化.
c++11之前用户定义的构造函数,可以在初始化列表里调用基类的构造函数,同时可以传参.基类的构造函数仍旧是先调用一遍.
c++11开始,就有了using Base::Base;的写法,当编译器在派生类里找不到相应的构造函数时,就会在这个基类Base里找.在此之后,会调用其它基类的默认构造函数,派生类的字段则是赋值初始化.(这也是c++11之后引入的)
Inheriting constructors
If the using-declaration refers to a constructor of a direct base of the class being defined (e.g. using Base::Base;), all constructors of that base (ignoring member access) are made visible to overload resolution when initializing the derived class.
If overload resolution selects an inherited constructor, it is accessible if it would be accessible when used to construct an object of the corresponding base class: the accessibility of the using-declaration that introduced it is ignored.
If overload resolution selects one of the inherited constructors when initializing an object of such derived class, then the
Base
subobject from which the constructor was inherited is initialized using the inherited constructor, and all other bases and members ofDerived
are initialized as if by the defaulted default constructor (default member initializers are used if provided, otherwise default initialization takes place). The entire initialization is treated as a single function call: initialization of the parameters of the inherited constructor is sequenced-before initialization of any base or member of the derived object.翻译下来大概是:
using Base::Base;的用途:
>派生类初始化时,基类的所有构造函数对overload resolution可见,无视权限修饰符.
>如果基类的某个构造函数被选中,那么先用它初始化那个基类,然后"其它的基类"和"派生字段"被初始化就像使用默认的default 构造函数.(采用提供的default initializers,若用户未提供,则发生默认初始化).
>那个基类的构造函数总是最先被调用.
下面是例子代码:
struct B1 { B1(int, ...) { } };
struct B2 { B2(double) { } }; int get(); struct D1 : B1 {
using B1::B1; // inherits B1(int, ...)
int x;
int y = get();
}; void test() {
D1 d(, , ); // OK: B1 is initialized by calling B1(2, 3, 4),
// then d.x is default-initialized (no initialization is performed),
// then d.y is initialized by calling get()
D1 e; // Error: D1 has no default constructor
} struct D2 : B2 {
using B2::B2; // inherits B2(double)
B1 b;
}; D2 f(1.0); // error: B1 has no default constructor
As with using-declarations for any other non-static member functions, if an inherited constructor matches the signature of one of the constructors of
Derived
, it is hidden from lookup by the version found inDerived
. If one of the inherited constructors ofBase
happens to have the signature that matches a copy/move constructor of theDerived
, it does not prevent implicit generation ofDerived
copy/move constructor (which then hides the inherited version, similar tousing operator=
).翻译:简单的说,就是using Base:Base之后,派生类可以重载某一个版本来覆盖掉. 基类的copy/move constructor不受这个using的影响,派生类如果没有copy/move constructor,编译器会生成默认的,不会用基类的.
例子代码:
struct B1 { B1(int); };
struct B2 { B2(int); }; struct D2 : B1, B2 {
using B1::B1;
using B2::B2;
D2(int); // OK: D2::D2(int) hides both B1::B1(int) and B2::B2(int)
};
D2 d2(); // calls D2::D2(int)
c++11的构造函数继承的更多相关文章
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- JS继承之借用构造函数继承和组合继承
根据少一点套路,多一点真诚这个原则,继续学习. 借用构造函数继承 在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术( ...
- javascript中继承(二)-----借用构造函数继承的个人理解
本人目录如下: 零.寒暄&回顾 一,借用构造函数 二.事件代理 三,call和apply的用法 四.总结 零.寒暄&回顾 上次博客跟大家分享了自己对原型链继承的理解,想看的同学欢迎猛击 ...
- javascript 封装 构造函数继承 非构造函数继承
1 封装 把"属性"(property)和"方法"(method),封装成一个对象,甚至要从原型对象生成一个实例对象 1.1 简单封装:var cat1 = { ...
- js继承之借用构造函数继承
我的上一篇文章介绍了,原型链继承模式.但是单纯的原型链模式并不能很好地实现继承. 一.原型链的缺点 1.1 单纯的原型链继承最大的一个缺点,来自于原型中包含引用类型的值. 本来,我们没有通过原型链实现 ...
- javascript实现继承3种方式: 原型继承、借用构造函数继承、组合继承,模拟extends方法继承
javascript中实现继承的三种方式:原型继承.借用构造函数继承.混合继承: /* js当中的继承 js中 构造函数 原型对象 实力对象的关系: 1 构造函数.prototype = 原型对象 2 ...
- js继承之组合继承(结合原型链继承 和 借用构造函数继承)
在我的前两篇文章中,我们已经介绍了 js 中实现继承的两种模式:原型链继承和借用构造函数继承.这两种模式都存在各自的缺点,所以,我们考虑是否能将这二者结合到一起,从而发挥二者之长.即在继承过程中,既可 ...
- Javascript继承2:创建即继承----构造函数继承
//声明父类 function SuperClass(id){ //值类型公有属性 this.id = id; //引用类型公有属性 this.books = ['Html','Css']; } // ...
- 深入浅出javascript(十二)继承——构造函数继承和组合继承
#题记: 有一水果类,抽象出属性包括:name(水果品种),price(价格),id(ID号).现有两个子类,分别为苹果,桔子,希望继承水果父类. 一.构造函数继承 构造函数继承相当把父类的属性在子类 ...
随机推荐
- kali网络源配置
使用vim对sources.list文件进行修改: $ vim /etc/apt/sources.list 随便挑选一个源添加到该文件中 ----------------------------- ...
- 英语单词contributors
contributors 来源——github网站 翻译 n. 贡献者:参与者:编著者(contributor的复数形式) TOEFL | GMAT 词根:cont ...
- JQUERY的$(function(){})和window.onload=function(){}的区别【转】
在Jquery里面,我们知道入口函数有两种写法:$(function(){}) 和$(document).ready(function(){}) 作用类似于传统JavaScript中的window.o ...
- 移动端与pc端的区别 及 ios的 兼容性问题
前言:这里移动端主要指 hybrid app 中的H5页面.app 中对页面 样式和功能 的需求会更精细一点. 1.适配: 手机端的尺寸多样,3.5英寸的 iPhone4应该是最小的,只要考虑 兼容到 ...
- tom
题目描述 众所周知,Tom 猫对香肠非常感兴趣.有一天,Tom 家里的女主人赏给了Tom 一大堆香肠.这些香肠太多了,以至于Tom 一顿吃不完,于是它把这些香肠串成了一棵树,树的每个节点上都有一个香肠 ...
- 关于设置shadowPath的重要性
这是超级容易添加阴影到iOS中的任何视图.所有您需要做的是 添加QuartzCore框架到项目中(如果不存在的话) 导入QuartzCore到您的执行文件 添加一行如[myView.layer set ...
- 20175126《Java程序设计》第十周学习总结
# 20175126 2016-2017-2 <Java程序设计>第十周学习总结 ## 教材学习内容总结 - 本周学习方式主要为手动敲代码并理解内容学习. -本周学习十二章,主要内容如下: ...
- Python Django 编写一个简易的后台管理工具1-安装环境
安装python环境 MAC 一般都会自带 Python2.x版本 的环境,你也可以在链接 https://www.python.org/downloads/mac-osx/ 上下载最新版安装. 安装 ...
- CommonJS规范 by ranyifeng
1,概述 CommonJS是服务器端模块的规范,Node.js采用了这个规范. 根据CommonJS规范,一个单独的文件就是一个模块.加载模块使用require方法,该方法读取一个文件并执行,最后返回 ...
- redux请求数据流程
redux请求数据流程 store里面的index.js文件 import {createStore,combineReducers,applyMiddleware} from "redux ...