[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
随机推荐
- 高性能mysql:创建高性能的索引
本文系阅读<高性能MySQL>,Baron Schwartz等著一书中第五章 创建高性能的索引的笔记,索引是存储引擎用于快速找到记录的一种数据结构. 索引对于良好的性能非常关键,尤其是当表 ...
- 在Notepad++中使用文本对比插件
目前Notepad++最新版是7.5.1,但很多插件仍然不能在64位版中使用,官网上是这么说的“Note that the most of plugins (including Plugin Mana ...
- java 整型数据转换为小数类型 BigDecimal 装换为Double
A,B为String类型 ,A-B=C BigDecimal A=(BigDecimal) map.get("A"); BigDecimal B=(BigDecimal) map. ...
- UIPickerView的简单使用
UIPickerView是一个选择器它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,使用也比较简单.下面做了一个关于天气预报的小Demo 用 UI ...
- 我的Tmux学习笔记
0. 修改指令前缀 // ~/.tmux.conf ubind C-b set -g prefix C-a 1. 新建会话 tmux tmux new -s session-name // 可以设置会 ...
- day 28 黏包及黏包解决方案
1.缓冲区 每个socket被创建以后,都会分配两个缓冲区,输入缓冲区和输出缓冲区,默认大小都是8k,可以通过getsocket()获取,暂时存放传输数据,防止程序在发送的时候卡阻,提高代码运行效率. ...
- 20145234黄斐《Java程序设计》第九周学习总结
教材学习内容总结 JDBC Java语言访问数据库的一种规范,是一套API.JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语 ...
- 成都Uber优步司机奖励政策(3月18日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- React-精华版
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...
- spring-boot、mybatis整合
一.MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 X ...