Initializer List is used to initialize data members of a class. The list of members to be initialized is indicated with constructor as a comma separated list followed by a colon.

  Following is an example that uses initializer list to initialize x and y of Point class.

 1 #include<iostream>
2 using namespace std;
3
4 class Point {
5 private:
6 int x;
7 int y;
8 public:
9 Point(int i = 0, int j = 0):x(i), y(j)
10 {
11 }
12 /* The above use of Initializer list is optional as the
13 constructor can also be written as:
14 Point(int i = 0, int j = 0)
15 {
16 x = i;
17 y = j;
18 }
19 */
20
21 int getX() const
22 {
23 return x;
24 }
25 int getY() const
26 {
27 return y;
28 }
29 };
30
31 int main()
32 {
33 Point t1(10, 15);
34
35 cout<<"x = "<<t1.getX()<<", ";
36 cout<<"y = "<<t1.getY();
37
38 return 0;
39 }
40
41 /* OUTPUT:
42 x = 10, y = 15
43 */

  The above code is just an example for syntax of Initializer list. In the above code, x and y can also be easily initialed inside the constructor.

  But there are situations where initialization of data members inside constructor doesn’t work and Initializer List must be used.

  Following are such cases:

  1) For initialization of non-static const data members: const data members must be initialized using Initializer List.

  In the following example, “t” is a const data member of Test class and is initialized using Initializer List.

 1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 const int t;
7 public:
8 Test(int t):t(t) //Initializer list must be used
9 {
10 }
11 int getT()
12 {
13 return t;
14 }
15 };
16
17 int main()
18 {
19 Test t1(10);
20 cout<<t1.getT();
21 return 0;
22 }
23
24 /* OUTPUT:
25 10
26 */

  2) For initialization of reference members:Reference members must be initialized using Initializer List.

  In the following example, “t” is a reference member of Test class and is initialized using Initializer List.

 1 // Initialization of reference data members
2 #include<iostream>
3 using namespace std;
4
5 class Test
6 {
7 int &t;
8 public:
9 Test(int &t):t(t)
10 {
11 } //Initializer list must be used
12 int getT()
13 {
14 return t;
15 }
16 };
17
18 int main()
19 {
20 int x = 20;
21 Test t1(x);
22 cout<<t1.getT()<<endl;
23 x = 30;
24 cout<<t1.getT()<<endl;
25 return 0;
26 }
27 /* OUTPUT:
28 20
29 30
30 */

  3) For initialization of member objects which do not have default constructor.

  In the following example, an object “a” of class “A” is data member of class “B”, and “A” doesn’t have default constructor. Initializer List must be used to initialize "a".

 1 #include <iostream>
2 using namespace std;
3
4 class A
5
6 {
7 int i;
8 public:
9 A(int );
10 };
11
12 A::A(int arg)
13
14 {
15 i = arg;
16 cout << "A's Constructor called: Value of i: " << i << endl;
17 }
18
19 // Class B contains object of A
20 class B
21
22 {
23 A a;
24 public:
25 B(int );
26 };
27
28 B::B(int x):a(x)
29
30 {
31
32 //Initializer list must be used
33 cout << "B's Constructor called";
34 }
35
36 int main()
37
38 {
39 B obj(10);
40 return 0;
41 }
42 /* OUTPUT:
43 A's Constructor called: Value of i: 10
44 B's Constructor called
45 */

  If class A had both default and parameterized constructors, then Initializer List is not must if we want to initialize “a” using default constructor, but it is must to initialize “a” using parameterized constructor.

  4) For initialization of base class members : Like point 3, parameterized constructor of base class can only be called using Initializer List.

 1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 int i;
7 public:
8 A(int );
9 };
10
11 A::A(int arg)
12 {
13 i = arg;
14 cout << "A's Constructor called: Value of i: " << i << endl;
15 }
16
17 // Class B is derived from A
18 class B: A
19 {
20 public:
21 B(int );
22 };
23
24 B::B(int x):A(x)
25 {
26 //Initializer list must be used
27 cout << "B's Constructor called";
28 }
29
30 int main()
31 {
32 B obj(10);
33 return 0;
34 }

  5) When constructor’s parameter name is same as data member
  If constructor’s parameter name is same as data member name then the data member must be initialized either using this pointer or Initializer List. In the following example, both member name and parameter name for A() is “i”.

 1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 int i;
7 public:
8 A(int );
9 int getI() const
10 {
11 return i;
12 }
13 };
14
15 A::A(int i):i(i)
16 {
17 } // Either Initializer list or this pointer must be used
18
19 /* The above constructor can also be written as
20 A::A(int i)
21 {
22 this->i = i;
23 }
24 */
25
26 int main()
27 {
28 A a(10);
29 cout<<a.getI();
30 return 0;
31 }
32 /* OUTPUT:
33 10
34 */

  6) For Performance reasons:
  It is better to initialize all class variables in Initializer List instead of assigning values inside body.

  Consider the following example:

 1 // Without Initializer List
2 class MyClass
3 {
4 Type variable;
5 public:
6 MyClass(Type a)
7 {
8 // Assume that Type is an already
9 // declared class and it has appropriate
10 // constructors and operators
11 variable = a;
12 }
13 };

  Here compiler follows following steps to create an object of type MyClass
  1. Type’s constructor is called first for “a”.
  2. The assignment operator of “Type” is called inside body of MyClass() constructor to assign

  variable = a;
  3. And then finally destructor of “Type” is called for “a” since it goes out of scope.

 1 // With Initializer List
2 class MyClass
3 {
4 Type variable;
5 public:
6 MyClass(Type a):variable(a)
7 {
8 // Assume that Type is an already
9 // declared class and it has appropriate
10 // constructors and operators
11 }
12 };

  Now consider the same code with MyClass() constructor with Initializer List With the Initializer List, following steps are followed by compiler:
  1. Copy constructor of “Type” class is called to initialize : variable(a). The arguments in initializer list are used to copy construct “variable” directly.
  2. Destructor of “Type” is called for “a” since it goes out of scope.

  As we can see from this example if we use assignment inside constructor body there are three function calls: constructor + destructor + one addition assignment operator call. And if we use Initializer List there are only two function calls: copy constructor + destructor call. See this post for a running example on this point.

  This assignment penalty will be much more in “real” applications where there will be many such variables. Thanks to ptr for adding this point.

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  10:36:51

When do we use Initializer List in C++?的更多相关文章

  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. JMeter学习笔记--关联

    1.什么是关联? 本次请求需要的数据,需要上一步的请求返回给提供的过程. 2.JMeter关联中常用的两种方式 正则表达式提取器 正则表达式提取器用于对页面任何文本的提取,提取的内容是根据正则表达式在 ...

  2. PTA 最小堆插入元素和删除堆顶(无哨兵元素) (20分)

    PTA 最小堆插入元素和删除堆顶(无哨兵元素) (20分) 对于给定的最小堆(优先队列),分别实现插入元素和删除堆顶的函数. 函数接口定义: int insertIntoHeap(struct Hea ...

  3. SDKStyle的Framework项目使用旧版项目文件生成的Nuget包遇到的问题

    随笔-2021-11-10 SDKStyle的Framework项目使用旧版项目文件生成的Nuget包遇到的问题 简介 C#从NetCore之后使用了新版的项目文件,SDK-Style项目,新版本的项 ...

  4. C++中简单使用HP-Socket

    目录 简介 使用方式 实现简单线程池 实现TCP客户端 实现TCP服务端 实现Http客户端 附件 简介 HP-Socket 是一套通用的高性能 TCP/UDP /HTTP 通信 框架 ,包含服务端组 ...

  5. HttpClient用法--这一篇全了解(内含例子)

    HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http协议的),提高了开发的效率,也 ...

  6. 第四周PTA笔记 好吃的巧克力+特殊的翻译+下次一定(续)+走迷宫

    好吃的巧克力 超市正在特价售卖巧克力,正好被贪吃的Lucky_dog看见了. 巧克力从左到右排成一排,一共有N个,M种. 超市有一个很奇怪的规定,就是你在购买巧克力时必须提供两个数字a和b,代表你要购 ...

  7. JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?

    1.innerHTML 属性 (参考自<JavaScript高级程序设计>294页) 在读模式下,innerHTML 属性返回与调用元素的所有子节点(包括元素.注释和文本节点)对应的 HT ...

  8. Windows应用开发中程序窗口中的各种图标尺寸规划

    为了让你的图标在各个视图模式下都能有合适的尺寸,需要制作4种尺寸16x16.32x32.48x48.256x256 在Windows系统中,几乎所有窗口都是ListView,其中的图标都按照指定的尺寸 ...

  9. go 错误处理设计思考

    前段时间准备对线上一个golang系统服务进行内部开源,对代码里面的错误处理进行了一波优化. 优化的几个原因: 错误处理信息随意,未分类未定义.看到错误日志不能第一时间定位 错误的日志重复,有时候一个 ...

  10. Trivy 容器镜像扫描工具学习

    简介 官方地址:https://github.com/aquasecurity/trivy Trivy是aqua(专注云原生场景下的安全)公司的一款开源工具,之前历史文章也有对aqua的一些介绍. T ...