Initialization of data members
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的更多相关文章
- 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 ...
- 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 ...
- Static data members in C++
Predict the output of following C++ program: 1 #include <iostream> 2 using namespace std; 3 4 ...
- [EffectiveC++]item22:Declare data members private
将成员变量隐藏在函数接口的背后,可以为“所有可能的实现”提供弹性, 假设我们有一个public成员变量,而我们最终取消了它,多少代码可能会被破坏呢?那是一个不可知的大量. protected成员变量就 ...
- 条款22:将成员变量声明为private(Declare data members private)
NOTE: 1.切记将成员变量声明为private.这可赋予客户访问数据的一致性 可细微划分访问控制 允诺约束条件获得保证,并提供class作者以充分的实现弹性. 2.protected 并不比pub ...
- 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 ...
- 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员
转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...
- Chapter 3.GDI/DirectDraw Internal Data Structures
说明,在这里决定跳过第二章,实在是因为里面涉及的内容太理论,对我而言又太艰深 3.1 HANDLES AND OBJECT-ORIRNTED PROGRAMMING In normal object- ...
- Effective C# 学习笔记(原则一:始终能的使用属性(property),而不是可直接访问的Data Member)
原则一:始终能的使用属性(property),而不是可直接访问的Data Member Always use properties instead of accessible data memb ...
随机推荐
- 【JavaScript】JS的坚实基础
前言 考虑到在后面的开发中,需要大量的使用js语言去进行开发,所以准备重新规整一下javascript的知识点,专门开了一个js的专栏,用来复习一下js语言.万事开头难,要是后面写的有问题的,欢迎 ...
- 羽夏看Win系统内核——驱动篇
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...
- [linux]centos7.4上安装MySQL-8.0.11【完美安装】
版本声明 centos7.4 MySQL-8.0.11 1.我用的阿里云的虚拟主机,刚从windows换到linux,需要装下常用工具 #安装下sz rz常用到上传下载的命令 yum install ...
- Python 匿名函数用法和return语句
匿名函数用法1.什么是匿名函数 函数定义过程中,没有给定名称函数,python中用lambda表达式创建匿名函数 lambda只是一个表达式,函数体比def简单 lambda主题是一 ...
- 【linux系统】java环境搭建
搭建步骤 1.安装java : 上传java安装包到linux系统----- rz jdk-8u202-linux-x64.tar.gz jdk下载地址:https://www.oracle.com/ ...
- 基于Guava API实现异步通知和事件回调
本文节选自<设计模式就该这样学> 1 基于Java API实现通知机制 当小伙伴们在社区提问时,如果有设置指定用户回答,则对应的用户就会收到邮件通知,这就是观察者模式的一种应用场景.有些小 ...
- [loj3285]Circus
将奶牛的状态用序列$\{a_{1},a_{2},...,a_{m}\}$来描述,其中$a_{i}$表示第$i$头奶牛的位置(奶牛数量为$m$) 下面,先来考虑对于某个特定的$m$如何处理: 对于一条简 ...
- [bzoj5338]xor
维护两颗可持久化字典树(当然可以放在一起),第一棵维护每一个点到根的每一位的二进制数量,在其父亲的基础上建立:第二棵维护dfs序上每一个点到第1个点的二进制数量,在其上一个点的基础上建立. 对于询问1 ...
- [cf720D]Slalom
对于每一行,这些障碍将其划分为若干段,记第$i$行($y=i$时)从左到右第$j$段为$[l_{i,j},r_{i,j}]$ 显然一条路径恰好经过每一行中的一段,且两种方案不同当且仅当其中经过的一段不 ...
- 第08章 MySQL聚合函数
第08章 MySQL聚合函数 我们上一章讲到了 SQL 单行函数.实际上 SQL 函数还有一类,叫做聚合(或聚集.分组)函数,它是对一组数据进行汇总的函数,输入的是一组数据的集合,输出的是单个值. 1 ...