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. injection for Xcode10使用方法

    对于一个使用Xcode的使用者来说,麻烦的地方在于使用代码布置界面时候的调试,5s改一下代码,用10s查看修改效果,如果电脑配置稍低,时间更长,这是病,得治,哈哈.下面就来说一下injection的使 ...

  2. luajit 64位 for cocos2dx 编译ios解决方法

    最近luajit发布了64位beta版,由于appstore上线必须是64位的应用,而且我的游戏项目用到lua脚本,所以必须要用到64位的luajit来编译lua脚本. 方法如下: 在luajit官网 ...

  3. Java设计模式六大原则-2

    Java设计模式六大原则-2 做Java程序开发的每天都在使用JDK,Spring,SpringMvc,Mybatis,Netty,MINA等框架,但很少有人懂得背后的原理.即使打开跟下原码也是一头雾 ...

  4. JavaScript Event Loop和微任务、宏任务

    为什么JavaScript是单线程? JavaScript的一大特点就是单线程, 同一时间只能做一件事情,主要和它的用途有关, JavaScript主要是控制和用户的交互以及操作DOM.注定它是单线程 ...

  5. WARNING: IPv4 forwarding is disabled. Networking will not work.

    1:Test environment [root@docker-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Cor ...

  6. Java : Netty 入门案例

    接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...

  7. [转]MySQL日期与时间戳常用函数

    本文原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜 ...

  8. vowels_单元音

    vowels(美式): 单元音: [i]:需要用劲喊出类似于“yi”的四声,费力咧开嘴,单词eat.need.thief.meet. [?]:卷舌音,单词bird.her.worry.certain. ...

  9. error: command 'aarch64-linux-gnu-gcc' failed with exit status 1

    使用jetson tx2安装tensorpack时报错: error: command 'aarch64-linux-gnu-gcc' failed with exit status 1 改正: 如果 ...

  10. 初识Tarjan

    Tarjan,一个十分有用的东西,可以求有向图的强连通分量,复杂度达到O(V+E). Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的节 ...