[C++] Class (part 2)
Members that are const or reference must be initialized. Similary, members that are of a class type that does not define a default constructor also must be initialized.
class ConstRef{
public:
ConstRef(int ii);
private:
int i;
const int ci;
int & ri;
};
ConstRef::ConstRef(int ii){
i = ii; // ok
ci = ii; // error: cannot assign to a const
ri = i; // error: ri was never initialized
}
By the time the constructor begin executing, initialization is completed. The correct way to wirtie this constructor is:
// explictly initialize reference and const members
ConstRef::ConstRef(int ii): i(ii), ci(ii), ri(i) { }
Members are initiazlied in the order in which they appear in the class definition, not the order in which they appear in the constructor initializer.
class X{
int i;
int j;
public:
// undefined: i is initialized before j
X(int val): j(val), i(j) { }
}
To avoid using members to initiazlie other members.
X(int val): j(val), i(val) { }
class Sales_data{
public:
// non-delegating constructor initialize member before corresponding argument
Sales_data(string s, unsigned cnt, double price): bookNo(s), units_sold(cnt), revenue(cnt * price) { }
// remaining constructors all delegate to another constructor
Sales_data(): Sales_data('', , ) { }
Sales_data(string s): Sales_data(s, , ){ }
Sales_data(istream & is): Sales_data() { read(is, *this); }
};
Had the function bodies contain code, that code would be run before control return to the function body of the delegating constructor.
In practice, it is almost right to provide a default constructor if other constructor is defined.
class NoDefault{
public:
NoDefault( const string &);
// additional members follow, but no other constructor
};
Struct A{
NoDefault my_mem;
};
A a; // error: cannot synthesize a constructor of A
Struct B{
B() { } // error: no initialization for b_member
Default b_member;
};
It is common mistake among programmers new to C++ to try to declare an object initialized with the default constructor as function
// opps! defines a function taking no parameter and returning an object of type Sales_data
Sales_data obj(); // define an object that use the default constructor
Sales_data obj;
Reference:
C++ Primer, Fifth Edition, chapter 7 Classes
随机推荐
- Ubuntu16 安装Anaconda3+tensorflow cpu版
打开火狐浏览器,下载anaconda安装包,网址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=D 下载完成,到Do ...
- 20181030noip模拟赛T1
YY的矩阵 YY有一个大矩阵(N*M), 矩阵的每个格子里都有一个整数权值W[i,j](1<=i<=M,1<=j<=N) 对于这个矩阵YY会有P次询问,每次询问这个大矩阵的一个 ...
- Python学习笔记六:集合
集合 Set,去重,关系测试:交.并.差等:无序 list_1=set(list_1), type(list_1) list_2=set([xxxxx]) 交集:list_1.intersectin( ...
- gmtime与mktime的重新编写
这几日发现Linux的mktime与gmtime所处理的数据类型是32位的,即代表时间设置只能设置到2038年,在公司的产品要实现这个时间的突破还是得自己写一个新的处理时间的函数. 作为一个刚毕业的程 ...
- Hangfire初探
Hangfire 是什么? Hangfire 是一个定时任务的管理后台,它拥有定时任务功能和及其相关的管理后台界面.Hangfire 原生使用 .NET 开发的,同时支持 .NET 和 .NET Co ...
- 踩坑留印,启动进程遇到报错:/proc/self/fd/9: 2: ulimit: bad number
启动进程,遇到报错: /proc/self/fd/9: 2: ulimit: bad number 分析配置文件内容没有错误. 怀疑可能是文件格式问题,在IDE里面查看,果然是windows格式.ID ...
- 2018南京网络赛L题:Magical Girl Haze(最短路分层图)
题目链接:https://nanti.jisuanke.com/t/31001 解题心得: 一个BZOJ的原题,之前就写过博客了. 原题地址:https://www.lydsy.com/JudgeOn ...
- 使用JAX-WS(JWS)发布WebService(二)
将项目改为maven工程,并发布到Tomcat: WebService常用到的注解以及作用: 发布过程中遇到的问题总结: 一.将项目改为maven工程,并发布到Tomcat: 继续上一篇,将代码完善成 ...
- Windows下MySQL多实例运行(转)
关键字:Windows下MySQL多实例运行 阅读前注意事项: 1.有的版本的data目录不直接放在mysql安装目录下,有可能在:C:\ProgramData\MySQL\MySQL Server ...
- STM32的System memory
Main Flash memory 是STM32内置的Flash,一般我们使用JTAG或者SWD模式下载程序时,就是下载到这个里面,重启后也直接从这启动程序. System memory 从系统存储器 ...