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 ...
随机推荐
- Devlop Win 8 and Windows Phone App for Microsoft Dynamics CRM
Microsoft Dynamics CRM App for Windows Phone http://www.windowsphone.com/en-us/store/app/dynamics-cr ...
- js关键字
这已经是我第二次遇到这个问题了..关于关键字,用来做函数的命名,,,在高清上没问题,标清上秒死...页面都出不来...wtf...做个记录.. js关键字http://www.itxueyuan.or ...
- firefox和chrome对于favicon.ico关于content-security-policy的不同处理
1.favicon.ico是网站的title图标 2.在设置CSP时,举例如下,表示只允许来源为https://my.alipay.com的图片,如果不是,则向指定的url(report.php)发出 ...
- redis整合spring @Bean写法
jedis是一款java连接redis的客户端,spring基于jedis进行了封装,提供了简洁的操作redis的方法. 使用maven进行管理jar包之间的依赖: <dependency> ...
- 如何用C#检查硬盘是否是固态硬盘SSD
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何用C#检查硬盘是否是固态硬盘SSD.
- javabean以及内省技术详解(转)
一.关于javabean javabean是固定写法的java类 书写格式为: 1)必须有无参构造函数 2)属性必须私有, 我们称为字段 3)提供标准的getter和setter 例: name 字段 ...
- CopyU!v2.2 增加对设备信息的识别
更新版本的CopyU!v2.2已经完成大部分功能的设计,主打升级功能“设备信息识别”已经基本完成,现在放上测试截图:
- JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类
<pre name="code" class="java"></pre><pre name="code" cl ...
- WinDbug抓取进程dump
安装WinDbug(包含在 Windows Kits-debugger 中)后,运行如下命令将会在 explorer.exe进程发生崩溃后抓取相应的内存数据到D盘根目录下的一个文件夹中 adplus. ...
- ThinkPHP3.1新特性: 多层MVC支持
ThinkPHP基于MVC(Model-View-Controller,模型-视图-控制器)模式,不过均支持多层(multi-Layer)设计. 模型(Model)层:默认的模型层由Model类构成, ...