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 ...
随机推荐
- IE-一根网线二台电脑上网
1 直连线 使用交叉线(橙白 橙 (蓝白) (绿 绿白) ( 蓝)宗白 棕) 2 如果主机原本是用ADSL拔号上网的话,就在主机的网络连接下的宽带连接右击属性>高级,在“Internet 连接共 ...
- [网络]关于公网IP的一些事
家里的每一个路由器配置里,都有一个公网Ip,即下图中的IP地址
- localstorage本地存储
前段时间项目上用到了本地存储,所以研究看了下,在这做下笔记. 本地存储是一个window的一个属性,分别是localStorage和sessionStorage,两者用法完全相同,只不过一个是sess ...
- phpstorm映射远程项目
项目要设置为default,否则自动更新会失败:type要选正确 development path和web path都要设置 options选项中选ctrl+s自动保存,且下方没告警
- 【玩转微信公众平台之六】 搭建新浪SAEserver
赶紧接上一篇继续讲. ------本篇将介绍怎样搭建 新浪SAEserver.猛戳 http://sae.sina.com.cn/1.先自己注冊一个账号,假设有新浪的账号,微博之类的都能够直接拿来用, ...
- Spring.NET学习笔记
http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_Index.html
- OllyDbg 使用笔记 (二)
OllyDbg 使用笔记 (二) 參考 书:<加密与解密> 视频:小甲鱼 解密系列 视频 TraceMe.exe下载地址:http://pan.baidu.com/s/1c0s2twO T ...
- RHCA442学习笔记-Unit13网络性能调整
UNIT 13 Essential Network Tuning 网络性能调整 目标:1. 应用队列技术最大化网络吞吐量 2. 调整TCP和non-TCP网络soc ...
- Java基础知识强化之IO流笔记49:IO流练习之 复制指定目录下指定后缀名的文件并修改名称的案例
1. 复制指定目录下指定后缀名的文件并修改名称的案例 需求:复制指定目录下的指定文件,并修改后缀名. • 指定的文件是:.java文件. • 指定的后缀名是:.jad • 指 ...
- javabean对象要实现的接口们和要重写的方法们
在使用list集合的时候,什么也不用. 原因:list允许存储重复的元素. 在使用set集合的时候,要重写,equals()方法 和 hashCode() 方法. 愿意:set集合 不允许存放相同的元 ...