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 ...
随机推荐
- Linux&C网络编程————“聊天室”
从上周到现在一直在完成最后的项目,自己的聊天室,所以博客就没怎么跟了,今天晚上自己的聊天室基本实现,让学长检查了,也有好些bug,自己还算满意,主要实现的功能有: 登录注册 附近的人(服务器端全部在线 ...
- k8s入坑之路(10)kubernetes coredns详解
概述 作为服务发现机制的基本功能,在集群内需要能够通过服务名对服务进行访问,那么就需要一个集群范围内的DNS服务来完成从服务名到ClusterIP的解析. DNS服务在kubernetes中经历了三个 ...
- [第二章]c++学习笔记1(类和对象的基础2)
类的成员函数可和类的定义分开写 类成员的可访问范围(private,public,protected) 具体使用方法 使用例
- ubuntu更換清華軟件源
打开软件源的编辑sudo gedit /etc/apt/sources.list 软件源: Ubuntu--更改国内镜像源(阿里.网易.清华.中科大) 打開軟件源文件進行修改: 使用 sudo vim ...
- java-TCP协议发送和接收数据
TCP协议接收数据的步骤: A:创建接收数据的Socket对象 创建对象的时候要指定端口 B:监听客户端连接 等待客户端连接 C:获取Socket对象的输入流(字节流) D:读数据,并显示在控制台 E ...
- [cf10E]Greedy Change
对于$w$的表示方案,可以用序列描述,即$x_{i}$表示第$i$种货币的数量 贪心策略得到的方案即是(对应序列)字典序最大的方案,并定义最优策略得到的方案为在最小化货币总数的基础上,(对应序列)字典 ...
- AOP实现方式一
1.创建相应的类 2.代码 service沿用前面的 增加两个log Log.java package com.shao.log; import org.springframework.aop.Met ...
- 基于 Docker 安装 RocketMQ
docker-compose.yml version: '3.5' services: rmqnamesrv: image: foxiswho/rocketmq:server container_na ...
- ppt的作用
ppt不重要,使用ppt的场合很重要. 演讲分享,答辩总结,商业竞标,新年计划,年终总结.
- 日程功能模块【从建模到代码实现】UML + JavaFX
结合 uml 所学和 Javafx 从建模到实现一个子功能模块 -- 日程管理.新手上路,类图到代码实现的过程还是很曲折但所幸收获颇丰,记录一下学习心得. 日程功能模块 最后成果 JAVAFX里面没有 ...