[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
随机推荐
- hashcode和equals区别
hashcode:对象的初始地址的整数表示 Java中的对象是JVM在管理,JVM会在她认为合适的时候对对象进行移动,比如,在某些需要整理内存碎片的GC算法下发生的GC.此时,对象的地址会变动,但ha ...
- java读取xml文件的四种方法
Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> ...
- linux 安全防护
一.禁止ROOT用户远程登录 linux中root用户是超级管理员,可以针对root用户暴力破解密码,这样很不安全,工作中我们一般禁止root用户直接远程登陆,开设一个或多个普通用户,只允许登陆普通用 ...
- 基于JQ的简版选项卡记录
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- animation(动画)设置
1.animation 动画 概念:当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果. 通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器: 规定 ...
- 【Spark】源码分析之RDD的生成及stage的切分
一.概述 Spark源码整体的逻辑(spark1.3.1): 从saveAsTextFile()方法入手 -->saveAsTextFile() --> saveAsHadoopFile ...
- 使用Wamp搭建Php本地开发环境,HBuilder调试
初涉Php,此处做点笔记,希望下次不要能够轻松应对,至少不要在入同一个坑 本文摘要: wamp和HBuilder和Mysql5.7的安装包 Wamp的使用,包括80端口,443端口的占用问题 HBui ...
- 典型的 ajax 异步请求及错误处理
$.ajax({ url: path + '/emergency/saveEmergency.do', async: false,//同步,会阻塞操作 type: 'POST',//PUT DELET ...
- STM32F407+STemwin学习笔记之STemwin移植补充Touch
原文地址:http://www.cnblogs.com/NickQ/p/8857213.html 环境:keil5.20 STM32F407ZGT6 LCD(320*240) STemwin:S ...
- 理解golang中的channel
channel是goroutine之间的通信机制.可以类比线程间的通信,线程间的通信有多种方式,比如线程上下文.共享内存.IPC通信.socket实现不同机器间的通信. channel用起来很简单,绑 ...