【C++ 补习】Copy Control

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.

转载于:https://www.cnblogs.com/Patt/p/11517942.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值