C++ Primer 5th edition, chapter 13.

The Rule of Three

  • If a class needs a destructor, it almost surely needs a copy constructor and copy-assignment operator as well.

  • If a class needs a copy constructor, it almost surely needs a copy-assignment operator, and vice versa.

The Copy and Swap Idiom

Classes that define swap often use swap to define their assignment operator. These operators use a technique known as copy and swap. This technique swaps the left-hand operand with a copy of the right-hand operand. The interesting thing about this technique is that it automatically handles self assignment and is automatically exception safe.

Rvalue References

An rvalue reference is a reference that must be bound to an rvalue. Rvalue references have the important property that they may be bound only to an object that is about to be destroyed. As a result, we are free to "move" resources from an rvalue reference to another object.

As we know, we cannot bind rvalue references to expressions that require a conversion, to literals, or to expressions that return an rvalue. Rvalue references have the opposite binding properties: We can bind an rvalue reference to these kinds of expressions, but we cannot directly bind an rvalue reference to an lvalue.

The Synthesized Move Operations

If a class defines its own copy constructor, copy-assignment operator, or destructor, the move constructor and move assignment operator are not synthesized. As a result, some classes do not have a move constructor or a move-assignment operator.

The compiler will synthesize a move constructor or a move-assignment operator only if the class doesn't define any of its own copy-control members and if every nonstatic data member of the class can be moved. The compiler can move members of built-in type. It can also move members of a class type if the members's class has the corresponding move operation.

【C++ 补习】Copy Control的更多相关文章

  1. [c++] Copy Control

    C++ allows the programmer to define how objects are to be copied, moved, assigned and destroyed. Tog ...

  2. Copy Control settings

    Copy Control settings     Skip to end of metadata   Created by Rajesh Banka, last modified by Jyoti ...

  3. [C++] Copy Control (part 1)

    Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...

  4. C/C++:copy control (拷贝控制)

    前言:当定义一个类的时候,我们显示或者隐式地指定在此类型的对象拷贝,移动,赋值,销毁时做些什么,一个类通过定义五种特殊的成员函数来控制这些操作,包括拷贝构造函数,拷贝赋值运算符,移动构造函数,移动赋值 ...

  5. C++之拷贝控制 (Copy Control)

    只有2种成员 值成员: 指针成员: 依实现可分为raw pointer / shared_ptr; 现在,仅考虑第③种:资源对象共享 角度来考虑拷贝控制 类的两种语义:值语义.似指针 编译器提供的de ...

  6. Bug 14143011 : ORA-19606: CANNOT COPY OR RESTORE TO SNAPSHOT CONTROL FILE

    Bug 14143011 : ORA-19606: CANNOT COPY OR RESTORE TO SNAPSHOT CONTROL FILE [oracle@test]$ tail -f rma ...

  7. C++-copy constructor、copy-assignment operator、destructor

    本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...

  8. [c++] Smart Pointers

    内存管理方面的知识 基础实例: #include <iostream> #include <stack> #include <memory> using names ...

  9. code of C/C++(3) - 从 《Accelerated C++》源码学习句柄类

    0  C++中多态的概念 多态是指通过基类的指针或者引用,利用虚函数机制,在运行时确定对象的类型,并且确定程序的编程策略,这是OOP思想的核心之一.多态使得一个对象具有多个对象的属性.class Co ...

随机推荐

  1. ThinkPHP系统常量

    _ROOT__ : 网站根目录地址 __APP__ : 当前项目(入口文件)地址 __URL__ : 当前模块地址 __ACTION__ : 当前操作地址 __SELF__ : 当前 URL 地址 _ ...

  2. 【CUDA 基础】4.2 内存管理

    title: [CUDA 基础]4.2 内存管理 categories: - CUDA - Freshman tags: - CUDA内存管理 - CUDA内存分配和释放 - CUDA内存传输 - 固 ...

  3. FTP服务器安装配置

    1.安装:yum install vsftpd -y 2.修改配置文件:cd /etc/vsftpd/ cat vsftpd.conf | grep -Ev '^$|^#' listen_port= ...

  4. Ubuntu安装之pycharm安装

    什么??公司要用Ubuntu(乌班图)?不会用??怎么进行python开发??? 乌班图操作系统下载地址:http://releases.ubuntu.com/18.04/ubuntu-18.04.1 ...

  5. JavaWeb-RESTful(三)_使用SpringMVC开发RESTful_下

    JavaWeb-RESTful(一)_RESTful初认识 传送门 JavaWeb-RESTful(二)_使用SpringMVC开发RESTful_上 传送门 JavaWeb-RESTful(三)_使 ...

  6. HTML标签功能分类

    按功能类别对HTML标签进行分类,源自HTML 参考手册 基础 标签 描述 <!DOCTYPE> 定义文档类型. html 定义 HTML 文档. title 定义文档的标题. body ...

  7. PHP-windows下安装

    下载 Apache下载地址:http://httpd.apache.org/download.cgi PHP下载地址:http://php.net/downloads.php 解压 解压到安装路径下H ...

  8. JS 浏览器地址栏传递参数,参数加密/解密(编码/解码)

    我们有时候在JS里进行页面跳转,并且传递了参数(AppName),如下: window.location = "../../views/form/edit.html?AppName=新增&q ...

  9. mysql基础知识语法汇总整理(二)

    mysql基础知识语法汇总整理(一) insert /*insert*/ insert into 表名(字段列表) values(值列表); --蠕虫复制 (优点:快速复制数据,测试服务器压力) in ...

  10. Python学习笔记—Dict和set

    dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子,假设要根据同学的名字 ...