C++11 introduced serveral contructor-related enhancements including:

  • Class member initializers
  • Delegating controctors

This article discusses about Class member initializers only.

Class member initializers are also called in-class initializers. C++11 follows other programming language that let you initializer a class member directly during its declaration:

Class M { // C++11
int j = ; // in-class initializer
bool flag(false); // another in-class initializer
public:
M();
}; M m1; // m1.j = 5, m1.flag = false

The complier transforms every member initializers(such as int j = 5) into a controctor's member initializer. Therefore, the declaration of class M above is semantically equalment to the following C++03 class definition:

class M2{
int j;
bool flag;
public:
M2(): j(), flag(false) {}
}

If the constructor includes an explict member initializer for a member that also has an in-class initializer, the controctor's member intializer will override the in-class initializer.

class M2{
int j = ; // in-class initializer
public:
M2(); // j = 7
M2(int i): j(i) {} // overrides j's in-class intializer
};
M2 m2; // j = 7
M2 m3(); // j = 5

Reference:

C++11 Tutorial: New Constructor Features that Make Object Initialization Faster and Smoother, smartbear.com

[C++] in-class initializer的更多相关文章

  1. 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception

    使用Spire组件抛出异常The type initializer for 'spr857' threw an exception 我使用免费的Spire.Xls组件尝试去转换Excel文档到PDF文 ...

  2. Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】

    1.问题:    在dealloc方法中使用[self.xxx release]和[xxx release]的区别? 用Xcode的Analyze分析我的Project,会列出一堆如下的提示:Inco ...

  3. C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法

    对于C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析,目前本人分析两种情况,如下: 情况一: 借鉴麒麟.NET ...

  4. System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

    [15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...

  5. iOS: 聊聊 Designated Initializer(指定初始化函数)

    iOS: 聊聊 Designated Initializer(指定初始化函数) 一.iOS的对象创建和初始化 iOS 中对象创建是分两步完成: 分配内存 初始化对象的成员变量 我们最熟悉的创建NSOb ...

  6. ios 修正waring:Method override for the designated initializer of the superclass '-init' not found

    swift引入后,为了使oc和swift更相近,对oc的初始化方法也进行了修正,具体说明,见下面的链接,这个waring的最简单的修正方法是,到相应类的头文件中,去掉在自定义初始化方法后面的 NS_D ...

  7. System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.

    08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...

  8. React和ES6(二)ES6的类和ES7的property initializer

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  9. initializer for conditional binding must have optional type not AVAudioPlayer

    if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") {    ...

  10. 正确编写Designated Initializer的几个原则

    Designated Initializer(指定初始化器)在Objective-C里面是很重要的概念,但是在日常开发中我们往往会忽视它的重要性,以至于我们写出的代码具有潜藏的Bug,且不易发现.保证 ...

随机推荐

  1. Vue和element-ui结合的简单使用

    前提 vue在前端技术中使用越来越多,也成为了主流框架,花点时间稍微了解下vue-cli.vue-router结合element-ui的使用.本人使用的是windows系统,后续介绍以windows7 ...

  2. Const 关键字详解

    const 标识符 在c++中作为常量修饰符 用来修饰 函数 变量  指针 const 修饰的变量不可以改变值 const 在修饰指针的时候 const 标识符出现在*的左边表示 指向的变量为常量不能 ...

  3. H3C Telnet 配置-01

    Telnet 配置管理方法是网络工程师和网络管理员使用最广泛的一种设备访问控制方法,它通过局域网或广域网实现本地或远程的访问控制,但是它的实验必须要求首先对设备进行初始化配置,否则用户无法正常登录和访 ...

  4. java 整型数据转换为小数类型 BigDecimal 装换为Double

    A,B为String类型 ,A-B=C BigDecimal A=(BigDecimal) map.get("A"); BigDecimal B=(BigDecimal) map. ...

  5. HTML5新标签兼容——> <!--<if lt IE 9><!endif-->

    第一种方法:(使用html5shiv) <!--[if lt IE9]> <script src="http://html5shiv.googlecode.com/svn/ ...

  6. 分析nginx 日志常用命令

    一.概念 并发连接数    客户端向服务器发起请求,并建立了TCP连接.每秒钟服务器链接的总TCP数量,就是并发连接数.请求数    请求数指的是客户端在建立完连接后,向http服务发出GET/POS ...

  7. JPMML解析PMML模型并导入数据进行分析生成结果

    JPMML解析Random Forest模型并使用其预测分析 导入Jar包 maven 的pom.xml文件中添加jpmml的依赖 <dependency> <groupId> ...

  8. buck型DC-DC分析

    BUCK型DC/DC电源分析 这种buck型DC/DC电路的拓扑结构:( 1N5822叫续流二极管!) LM2756相当于高速断开和闭合的开关,连接在Vin与Vout脚之间. 1. 在开关闭合时,电流 ...

  9. python爬虫#数据存储#JSON/CSV/MYSQL/MongoDB/

    Json数据处理 JSON支持数据格式: 对象(字典).使用花括号. 数组(列表).使用方括号. 整形.浮点型.布尔类型还有null类型. 字符串类型(字符串必须要用双引号,不能用单引号). 多个数据 ...

  10. 《PHP框架Laravel学习》系列分享专栏

    <PHP框架Laravel学习>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201735.html 文章 Laravel教程:l ...