In C++, class variables are initialized in the same order as they appear in the class declaration.

  Consider the below code.

 1 #include<iostream>
2
3 using namespace std;
4
5 class Test
6 {
7 private:
8 int y;
9 int x;
10 public:
11 Test() : x(10), y(x + 10)
12 {
13 }
14 void print();
15 };
16
17 void Test::print()
18 {
19 cout<<"x = "<<x<<" y = "<<y;
20 }
21
22 int main()
23 {
24 Test t;
25 t.print();
26 getchar();
27 return 0;
28 }

  The program prints correct value of x, but some garbage value for y, because y is initialized before x as it appears before in the class declaration.

  So one of the following two versions can be used to avoid the problem in above code.

 1 // First: Change the order of declaration.
2 class Test
3 {
4 private:
5 int x;
6 int y;
7 public:
8 Test() : x(10), y(x + 10)
9 {
10 }
11 void print();
12 };
13
14 // Second: Change the order of initialization.
15 class Test
16 {
17 private:
18 int y;
19 int x;
20 public:
21 Test() : x(y-10), y(20)
22 {
23 }
24 void print();
25 };

  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:23:29

Initialization of data members的更多相关文章

  1. ISO 9141-2 and ISO 14230-2 INITIALIZATION and DATA TRANSFER

    http://ecad.tu-sofia.bg/et/2005/pdf/Paper097-P_Dzhelekarski1.pdf INITIALIZATION Prior to any diagnos ...

  2. c# Use Properties Instead of Accessible Data Members

    advantage of properties: 1 properties can be used in data binding, public data member can not. 2 dat ...

  3. Static data members in C++

    Predict the output of following C++ program: 1 #include <iostream> 2 using namespace std; 3 4 ...

  4. [EffectiveC++]item22:Declare data members private

    将成员变量隐藏在函数接口的背后,可以为“所有可能的实现”提供弹性, 假设我们有一个public成员变量,而我们最终取消了它,多少代码可能会被破坏呢?那是一个不可知的大量. protected成员变量就 ...

  5. 条款22:将成员变量声明为private(Declare data members private)

    NOTE: 1.切记将成员变量声明为private.这可赋予客户访问数据的一致性 可细微划分访问控制 允诺约束条件获得保证,并提供class作者以充分的实现弹性. 2.protected 并不比pub ...

  6. When do we use Initializer List in C++?

    Initializer List is used to initialize data members of a class. The list of members to be initialize ...

  7. 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员

    转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...

  8. Chapter 3.GDI/DirectDraw Internal Data Structures

    说明,在这里决定跳过第二章,实在是因为里面涉及的内容太理论,对我而言又太艰深 3.1 HANDLES AND OBJECT-ORIRNTED PROGRAMMING In normal object- ...

  9. Effective C# 学习笔记(原则一:始终能的使用属性(property),而不是可直接访问的Data Member)

    原则一:始终能的使用属性(property),而不是可直接访问的Data Member    Always use properties instead of accessible data memb ...

随机推荐

  1. 一个校验接口引发的思考--我真的了解Response吗

    一个校验接口 最近,我需要对接一个外部接口,基本功能是:校验指定的门店是否完善了货运信息.接口大致是这样的: POST https://******/Dealer/CheckCarrier Heads ...

  2. ES6基础知识(async 函数)

    1.async 函数是什么?一句话,它就是 Generator 函数的语法糖. const fs = require('fs'); const readFile = function (fileNam ...

  3. Maven 依赖调解源码解析(一):开篇

    本文是系列文章<Maven 源码解析:依赖调解是如何实现的?>第一篇,主要做个开头介绍.并为后续的实验做一些准备.系列文章总目录参见:https://www.cnblogs.com/xia ...

  4. 如何看待 SAE 在2014 年 3 月 24 日发生的的大面积宕机事故?

    3 月 24 日晚间大约 23 点左右,新浪云 SAE 一处核心机柜掉电,导致 SAE 平台下大量应用无法正常访问,并在 10 小时后才陆续修复.这次事故暴露 SAE 的哪些缺陷?SAE 运维人员又是 ...

  5. [luogu7740]机器人游戏

    考虑容斥,令$f(S)$为要求$\forall p\in S,p$可以作为起点的方案数,答案即$\sum_{S\subseteq[0,n)}(-1)^{|S|}f(S)$ 关于计算$f(S)$,对于第 ...

  6. [atARC086F]Shift and Decrement

    将$A$操作看作直接除以2(保留小数),最终再将$a_{i}$取整 记$k$表示$A$操作的次数,$p_{i}$表示第$i$次$A$和第$i+1$次$A$之间$B$操作的次数(特别的,$p_{0}$为 ...

  7. 微信小程序中途加入云开发之坑

    一开始未使用云开发的小程序项目,之后想使用云开发能力时,要先删除对应在开发者工具中的项目(先压缩备份源码!),再用开发者工具重新创建,很多时候都需要用这种方式进行处理

  8. CF1368E Ski Accidents

    读懂题是第一要素. 考虑把点集分割为:\(A,B,C\) 首先把所有入度为\(0\)的点加入\(A\) 然后对所有入边只来自\(A\)的点加入\(B\) 然后对所有入边只来自\(B\)的点加入\(C\ ...

  9. Codeforces 1097G - Vladislav and a Great Legend(第二类斯特林数+树上背包)

    Codeforces 题目传送门 & 洛谷题目传送门 首先看到这题我的第一反应是:这题跟这题长得好像,不管三七二十一先把 \(k\) 次方展开成斯特林数的形式,\(f(X)^k=\sum\li ...

  10. DirectX12 3D 游戏开发与实战第十二章内容

    12.几何着色器 如果不启用曲面细分,那么几何着色器这个可选阶段将会在位于顶点着色器和像素着色器之间.顶点着色器以顶点作为输入数据,而几何着色器以完整的图元为输入数据.与顶点着色器不同的是,顶点着色器 ...