References & the Copy-Constructor
1 There are certain rules when using references: (Page 451)
A reference must be initialized when it is created. (Pointers can be initialized at any time.)
Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.)
You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
2 References in functions (Page 452)
When a reference is used as a function argument, any modification to the reference inside the function will cause changes to the argument outside the function.
3 Const references (Page 453)
If you know the function will respect the constness of an object, making the arguement a const reference will allow the function to be used in all situations. For built-in types, the function will not modify the argument, and for user-defined types, the function will call only const member functions, and won't modify any public data members.
Temporary objects are always const, so if you donot use a const reference, that argument wonot be accepted by the compiler.
4 Argument-passing guidelines (Page 455)
The efficiency savings can be substantial for such a simple habit: to pass an argument by value requires a constructor and destructor call, but if you are not going to modify the argument then passing by const refecence only needs an address pushed on the stack.
5 Copy-construction (Page 463)
If you create a copy-construction, the compiler will not perform a bitcopy when creating a new object from an existing one. It always call your copy-constructor.
6 Temporary objects
7 Default copy-constructor
8 Alternatives to copy-construction
You need a copy-constructor only if you are going to pass an object of your class by value. If that never happens, you donot need a copy-constructor.
9 Preventing pass-by-value
There is a simple technique for preventing pass-by-value: declare a private copy-constructor. You donot even need to create definition, unless one of your member functions or a friend function needs to perform a pass-by-value. If the user tries to pass or return the object by value, the compiler will produce an error message because the copy-constructor is private.
References & the Copy-Constructor的更多相关文章
- Copy Constructor in Java
Reference: TutorialPoints, GeekforGeeks The copy constructor is a constructor which creates an objec ...
- Copy constructor vs assignment operator in C++
Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...
- C++-copy constructor、copy-assignment operator、destructor
本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...
- 构造函数语义学之Copy Constructor构建操作(2)
二.详述条件 3 和 4 那么好,我又要问大家了,条件1 和 2比较容易理解.因为member object或 base class 含有copy constructor.那么member objec ...
- 构造函数语义学之Copy Constructor构建操作(1)
一.Copy Constructor的构建操作 就像 default constructor 一样,如果class没有申明一个 copy constructor,就会隐含的声明或隐含的定义一个.生成的 ...
- no copy constructor available or copy constructor is declared 'explicit'
今天新写了一个类.然后对这个类使用STL中的vector,碰到错误: no copy constructor available or copy constructor is declared 'ex ...
- Copy Constructor的构造操作
Copy Constructor的构造操作 有三种情况,会以一个object的内容作为另一个class object的初值: 1. 对一个object做显式的初始化操作 class X{…}; X ...
- Effective C++ 第0章 copy constructor和copy assignment operator
拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...
- copy constructor
copy constructor也分为trivial和nontrivial两种 如果class展现出bitwise copy semantics(按位拷贝语义),则不会构造出 copy constru ...
- c++官方文档-copy constructor
#include <iostream> using namespace std; class Example5 { string* ptr; public: Example5(const ...
随机推荐
- 一分钟快速入门openstack
一.它是什么,能干什么想认识一个事物,必须先弄明白它是什么,能干什么.首先说一下,openstack是一个搭建云平台的一个解决方案,说他不是个软件,但是我觉得说是一个软件,能够让大家认识更清晰些.op ...
- 关闭utorrent的广告
版本:3.4.9 / 方法来源:wikihow. 在"选项-高级"里将下面的选项全部改成false. offers.left_rail_offer_enabledoffers.sp ...
- iOS 开发中使用到的小技巧汇总
国庆即将来到,一个小项目也即将完成,把自己在项目中用的一些小技巧写出来,方便查找. 1,去掉分割线--动画设置透明度alpha //去掉tableView的分隔线: self.tableVie ...
- 使用freemarker生成html
http://herryhaixiao.iteye.com/blog/677524 由于freemarker这个技术很久很久就有了,注释我就没写得很详细了,相信大家都看得懂.下面就直接上代码以及一些代 ...
- python学习(4)
python(4)4.1 高阶函数:map/reduce map:实际上map也是一个函数,只不过他可以在参数里面包含别的函数.他有两种参数:第一种:函数(作用在后面要说的序列上),第二种:一个序列 ...
- 3D案例,导航,导航升级版
/*****************************百度钱包旋转变内容******************************/ <!DOCTYPE html> <htm ...
- Cocos2d-x 2.0 自适应多种分辨率
转自:http://dualface.github.io/blog/2012/08/17/cocos2d-x-2-dot-0-multi-resolution/ cocos2d-x 2.0 提供一个极 ...
- Ubuntu安装一:VM安装具体解释
1.下载VM中文版:http://download.pchome.net/system/sysenhance/down-4673-1.html,解压后双击安装包: 2.点击:下一步 3.允许安装协议, ...
- MySQL对于datetime 源码分析
http://tsecer.blog.163.com/blog/static/150181720160117355684/ 一.时间比较的语法分析 在mysql中,通常时间是一个必不可少的类型,而 ...
- iostat来对linux硬盘IO性能进行了解
http://www.php-oa.com/2009/02/03/iostat.html