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. iOS小知识点

    1.子视图超出frame的部分不显示  view.clipsToBounds 设置为YES;   UIScrollview的clipsToBounds默认就是YES 2.UIImage 有一个属性叫s ...

  2. chromium之message_pump_win之三

    上一篇分析MessagePumpForUI,参考chromium之message_pump_win之二 MessagePumpForIO,同MessagePumpForUI,也是要实现三个函数 // ...

  3. ATM购物作业

    一. 基本需求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录日常消费 ...

  4. HTML+css 文字只显示一行

    电脑端 设置行高,超出隐藏 p{ width: 80%; height: 16px; line-height: 16px; display: block; overflow: hidden; text ...

  5. mqtt使用二(集成到java代码中)

    1.我采用的是springboot,首先pom文件中添加mqtt需要用到的依赖 <dependency> <groupId>org.springframework.boot&l ...

  6. Redis在Linux中的运用

    Redis在Linux中的运用 一.Redis安装部署 下载: wget http://download.redis.io/releases/redis-3.2.12.tar.gz 解压: 上传至 / ...

  7. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

  8. go基础语法-函数

    1.基础定义 golang的函数很'纯粹',只有可变参数列表的概念,没有默认参数.可选参数.函数重载.操作符重载这些难以把控的概念 语法:'func'声明,而后函数名在前,中间的括号内定义参数,返回值 ...

  9. PHP.47-TP框架商城应用实例-后台22-权限管理-角色和管理员的关系

    角色和管理员的关系 角色功能 管理员功能 角色与管理的关联要通过管理-角色表进行{多对多} /********* 管理-角色表 *********/ drop if exists p39_admin_ ...

  10. 20154327 Exp4 恶意代码分析

    基础问题回答 (1)如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. 监控网络连接 监控是否创建新的进程 监控 ...