[C++] in-class initializer
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++] in-class initializer的更多相关文章
- 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception
使用Spire组件抛出异常The type initializer for 'spr857' threw an exception 我使用免费的Spire.Xls组件尝试去转换Excel文档到PDF文 ...
- Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】
1.问题: 在dealloc方法中使用[self.xxx release]和[xxx release]的区别? 用Xcode的Analyze分析我的Project,会列出一堆如下的提示:Inco ...
- C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法
对于C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析,目前本人分析两种情况,如下: 情况一: 借鉴麒麟.NET ...
- 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 ...
- iOS: 聊聊 Designated Initializer(指定初始化函数)
iOS: 聊聊 Designated Initializer(指定初始化函数) 一.iOS的对象创建和初始化 iOS 中对象创建是分两步完成: 分配内存 初始化对象的成员变量 我们最熟悉的创建NSOb ...
- ios 修正waring:Method override for the designated initializer of the superclass '-init' not found
swift引入后,为了使oc和swift更相近,对oc的初始化方法也进行了修正,具体说明,见下面的链接,这个waring的最简单的修正方法是,到相应类的头文件中,去掉在自定义初始化方法后面的 NS_D ...
- System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.
08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...
- React和ES6(二)ES6的类和ES7的property initializer
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- initializer for conditional binding must have optional type not AVAudioPlayer
if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") { ...
- 正确编写Designated Initializer的几个原则
Designated Initializer(指定初始化器)在Objective-C里面是很重要的概念,但是在日常开发中我们往往会忽视它的重要性,以至于我们写出的代码具有潜藏的Bug,且不易发现.保证 ...
随机推荐
- 如何应用SPRITEKIT的CAMERA实现游戏中的ENDLESS无限循环背景
A Tutorial For How To Use SpriteKit Camera Making Endless Background Player运用Camera节点向前移动的效果 向前舞动 命为 ...
- App 运行后屏幕顶部和底部各留黑边问题 - iOS
App 启动后屏幕的顶部和底部各产生一条黑边,App 的内容会被压缩在两个黑条内显示,比例失调的情况. 初步判断是启动页资源图片适配问题. 首先,查看工程 project => General ...
- c#一个日志类(log4net)
这个类就是对log4net的使用,就不多说了,但是看见网上的一个封装,自己用了下,感觉还不错,直接记录在这里.把自己使用的类直接贴出来. using log4net; using log4net.Co ...
- sql 优化的几种方法
.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. .应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而 ...
- Jqgrid利用正则匹配表达式正确移除html标签
在使用JqGrid表格插件过程中,遇到一个问题:后台取出来的字段是带有Html标签的,于是将内容填充到表格之后,带有的html标签会把表格撑开或者每一行的内容显示不统一,导致非常难看,就像下图所示: ...
- Python提高篇
Python提高篇 1.模块 1)模块定义 模块就是py文件,可以是你自己写的,也可以是python当中自带的工具,当你在某个py文件下想要引用其他模块的功能,就需要你把你把该py文件导入. 2)导入 ...
- 关于windows下安装mysql数据库出现中文乱码的问题
首先需要在自己安装的mysql路径下新建一个my.ini文件,如下: 然后在my.ini文件中输入一下内容,主要控制编码问题的为红框部分,如下: 为了方便大家使用,可以复制以下代码: [WinMySQ ...
- 第5天 Java基础语法
第5天 Java基础语法 今日内容介绍 方法 方法 方法概述 在我们的日常生活中,方法可以理解为要做某件事情,而采取的解决办法. 如:小明同学在路边准备坐车来学校学习.这就面临着一件事情(坐车到学校这 ...
- VS中添加lib与dll
参考与拓展阅读:https://blog.csdn.net/u012043391/article/details/54972127 lib: 1.附加包含目录---添加工程的头文件目录: ...
- (数据科学学习手札42)folium进阶内容介绍
一.简介 在上一篇(数据科学学习手札41)中我们了解了folium的基础内容,实际上folium在地理信息可视化上的真正过人之处在于其绘制图像的高度可定制化上,本文就将基于folium官方文档中的一些 ...