zoukankan      html  css  js  c++  java
  • 【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.

  • 相关阅读:
    CF1137FMatches Are Not a Child‘s Play【LCT】
    P4491[HAOI2018]染色【多项式,二项式反演】
    P3170[CQOI2015]标识设计【插头dp】
    log4j 使用教程说明
    log4j中Logger.getLogger()加载一个类提示错误
    编程基础 0x00008 的0x代表什么?
    编程基础 快速的进行 2进制,10进制,16进制 的 相互转换
    Java 基础 equals,hashcode和==的区别
    位运算 左移右移运算符 >>, <<, >>>
    Java 虚拟机 2.2 运行时数据区 Runtime Data Area
  • 原文地址:https://www.cnblogs.com/Patt/p/11517942.html
Copyright © 2011-2022 走看看