[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 = 5;    // 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(5), 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 = 7;    // in-class initializer
public:
    M2();    // j = 7
    M2(int i): j(i) {}    // overrides j‘s in-class intializer
};
M2 m2;     // j = 7
M2 m3(5);    // j = 5

Reference:

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

时间: 2024-08-25 16:55:54

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

ORB-SLAM2学习4 initializer.h

orb-slam2   Inlitializer.h  头文件的学习 a.预备知识,主要读取了作者的论文. 作者在论文中提到,这个头文件定义的东西全是用于单目的,双目和RGBD不需要初始化.地图初始化的目的就是得到两帧的相对运动,R  T,  然后根据 R T 和这两帧匹配好的关键点,三角化出初始的三 维地图.论文里面提到了一共有5步. 1.在当前帧与参考帧之间提取ORB特征,就行匹配. 2.开启两个同步线程,同时计算H矩阵和F矩阵,H=homography 用于特征点都是在同平面的情况,F=f

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

iOS: 聊聊 Designated Initializer(指定初始化函数) 一.iOS的对象创建和初始化 iOS 中对象创建是分两步完成: 分配内存 初始化对象的成员变量 我们最熟悉的创建NSObject对象的过程: 苹果官方有一副图片更生动的描述了这个过程: 对象的初始化是一个很重要的过程,通常在初始化的时候我们会支持成员变量的初始状态,创建关联的对象等.例如对于如下对象: 1 @interface ViewController : UIViewController 2 3 @end 4

[Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句

条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> features(const Widget& w); // OK bool highPriority = features(w)[5]; processWidget(w, highPriority); // ERROR auto highPriority = features(w)[5]; processWid

Cannot find an initializer for type &#39;[(String)]&#39; that accepts an argument list of type &#39;(LazyForward

Swift编译错误: Cannot find an initializer for type '[(String)]' that accepts an argument list of type '(LazyForwardCollection<MapCollectionView<Dictionary<Int, String>, Int>>)' 错误代码: var dict = [1: "m", 2: "i", 3: "i

debian The type initializer for &#39;System.Drawing.KnownColors&#39; threw an exception

Change the "System.Drawing" reference of "CoreCompat.System.Drawing"if you throw The type initializer for 'System.Drawing.KnownColors' threw an exception to do:apt-get install libgdiplus debian The type initializer for 'System.Drawing.

Designated Initializer

一个类,可能有很多初始化函数,但是有主次之分,最主要的初始函数应该对类内应当需要初始化的变量进行初始化.这个最主要的初始函数即Designated Initializer(指定初始化器),可以理解为是类的默认初始函数.比如,UIView的Designated Initializer是initWithFrame:而不是init: 原则1.类的正确初始化过程应当依次调用子类到父类的Designated Initializer.即使是用父类的Designated Initializer初始化一个子类对

【转】gcc warning: braces around scalar initializer (标量初始化的括号)

原文网址:http://stackoverflow.com/questions/3462513/gcc-warning-braces-around-scalar-initializer I have look-up-table as defined below and I'm making use of GCC. When I compile I get warnings as warning: braces around scalar initializer What does this wa

.net 调用 Matlab生成dll出现的问题(The type initializer for &#39;MathWorks?.MATLAB.NE?T.Utility.?MWMCR&#39; threw an exception.)

https://cn.mathworks.com/matlabcentral/answers/278399-i-get-an-error-saying-the-type-initializer-for-mathworks-matlab-net-utility-mwmcr-threw-an-except 这篇文章帮忙解决了问题 记录一下备忘: Hi Nug, Ensure that you have the MCR version corresponding to MATLAB 2012b ins

initializer element is not constant 问题

在Ubuntu下,比葫芦画瓢,写了一个程序,居然报错!!!! 1 #include <stdio.h> 2 3 float i = 3; 4 int j = *(int *)(&i) ; 5 6 int main (int argc , char *argv[]) 7 { 8 printf( "i = %f \n" , i ) ; 9 printf( "j = %#x \n" , j); 10 return 0; 11 } 对于高手,一眼就能看出

The type initializer for &#39;System.Data.Entity.Internal.AppConfig&#39; threw an exception.

当使用EF时,配置文件中会多出下面的配置代码: <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.C