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. vue-cli3项目中引入jquery 以及如何引进bootstrap

    1.安装jquery npm install jquery --save 2.或则在package.json中指定版本号,然后运行npm install命令 "dependencies&qu ...

  2. 使用fiddler实现手机抓包

    手机上无法直接查看网络请求数据,需要使用抓包工具.Fiddler是一个免费的web调试代理,可以用它实现记录.查看和调试手机终端和远程服务器之间的http/https通信. fiddler没有手机客户 ...

  3. POJ1990--POJ 1990 MooFest(树状数组)

    Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 Description Every year, ...

  4. From 7.22 To 7.28

    From 7.22 To 7.28 大纲 竞赛 我们好像要跟队爷考试... 考试的时候做题吧 学科 还是跟之前一样吧, 完型和阅读几乎没做过... 运动 踢足球!!!!!! 可惜bb他们去上海了... ...

  5. ajax 415

    ajax 发送post请求是出现415错误,是ajax的格式有问题,如下. $.ajax({ type: 'POST', url: '/login', data: { "username&q ...

  6. vscode如何使用命令面板

    vscode如何使用命令面板 方法/步骤     首先找到vscode.   进入,打开页面.   找到查看.   打开找到命令面板.   选择打开,可以看到命令.   下拉还有多个,以及快捷键.   ...

  7. 重入锁 ReentrantLock (转)(学习记录)

    重入锁(ReentrantLock)是一种递归无阻塞的同步机制.以前一直认为它是synchronized的简单替代,而且实现机制也不相差太远.不过最近实践过程中发现它们之间还是有着天壤之别. 以下是官 ...

  8. Linux下深度学习常用工具的安装

    .Matlab 2015 64bit 的安装 (一)安装包下载 百度网盘: [https://pan.baidu.com/s/1gf9IeCN], 密码: 4gj3 (二)Vmware 使用Windo ...

  9. PreparedStatement执行sql語句

    import com.loaderman.util.JdbcUtil; import java.sql.Connection; import java.sql.PreparedStatement; i ...

  10. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

    System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...