Copy assignment operator

来自cppreference.com

 
 
C + +语言
大会的主题
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件执行语句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代语句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
跳转语句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函数声明
lambda函数的声明
函数模板
的历史。内嵌说明
异常规范 (过时了)
noexcept说明 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空间
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
规范
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存储时间说明符
constexpr说明 (C++11)
汽车符 (C++11)
alignas说明 (C++11)
初始化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
布尔文字
nullptr (C++11)
用户定义的 (C++11)
表达式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
另一种表示形式
实用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
声明类型别名 (C++11)
属性 (C++11)
施放
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
隐式转换
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-风格和功能转换
内存分配
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类特定的功能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
虚函数
覆盖说明 (C++11)
最后说明 (C++11)
明确的 (C++11)
静态的
特殊的成员函数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
复制的任务
移动分配 (C++11)
析构函数
模板
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类模板
函数模板
模板特化
参数包 (C++11)
杂项
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
内联汇编
 
一个拷贝赋值运算符的类T是一个非模板的非静态成员函数的名称operator=,它只有一个参数的类型TT&const T&volatile T&,或const volatile T&。与公共拷贝赋值运算符是A型CopyAssignable.
Original:
A copy assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T, T&, const T&, volatile T&, or const volatile T&. A type with a public copy assignment operator is CopyAssignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 语法

class_name & class_name :: operator= ( class_name ) (1) (C++11 起)
class_name & class_name :: operator= ( const class_name & ) (2) (C++11 起)
class_name & class_name :: operator= ( const class_name & ) = default; (3) (C++11 起)
class_name & class_name :: operator= ( const class_name & ) = delete; (4) (C++11 起)

[编辑] 解释

copy-and-swap idiom可以使用#典型的拷贝赋值运算符的声明
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom can be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#典型的拷贝赋值运算符时不能使用复制和交换成语声明
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#强制编译器生成的一个拷贝赋值运算符
Original:
# Forcing a copy assignment operator to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#避免隐式的拷贝赋值
Original:
# Avoiding implicit copy assignment
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
选择重载决议,例如拷贝赋值运算符时,被称为一个对象时,会出现在赋值表达式的左侧.
Original:
The copy assignment operator is called whenever selected by 重载决议, e.g. when an object appears on the left side of an assignment expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 隐式声明的拷贝赋值运算符

如果没有用户定义的复制赋值运算符的类类型(structclass,或union),编译器将声明一个内联类的成员。这隐式声明的拷贝赋值运算符的形式T& T::operator=(const T&)以下是真实的
Original:
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T& T::operator=(const T&) if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • 每个直接基BT有一个拷贝赋值运算符的参数是Bconst B&const volatile B&
    Original:
    each direct base B of T has a copy assignment operator whose parameters are B or const B& or const volatile B&
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 每个非静态数据成员MT类类型或类类型的数组有一个拷贝赋值运算符的参数是Mconst M&const volatile M&
    Original:
    each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M& or const volatile M&
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
否则隐式声明的拷贝赋值运算符被声明为T& T::operator=(T&)。 (请注意,由于这些规则,隐式声明的拷贝赋值运算符不能绑定到动荡的左值参数)
Original:
Otherwise the implicitly-declared copy assignment operator is declared as T& T::operator=(T&). (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
一个类可以有多个拷贝赋值运算符,如: T& T::operator=(const T&)T& T::operator=(T)。如果一些用户自定义的拷贝赋值运算符都存在,用户仍然可以强制生成的隐式声明的拷贝赋值运算符与关键字default.
Original:
A class can have multiple copy assignment operators, e.g. both T& T::operator=(const T&) and T& T::operator=(T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
因为拷贝赋值运算符的任何类,基类的赋值运算符总是宣称永远是隐藏的。如果一个声明是用来把从基类中的赋值运算符,它的参数类型可以是相同的参数类型的隐式派生类的赋值运算符,声明还隐藏着由隐声明.
Original:
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 删除隐式声明的拷贝赋值运算符

隐式声明的或默认的拷贝赋值运算符类T被定义为“删除”以下是正确的:
Original:
The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T有一个非静态数据成员是const
    Original:
    T has a non-static data member that is const
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T有一个非静态数据成员的引用类型.
    Original:
    T has a non-static data member of a reference type.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T有一个非静态数据成员不能复制分配(已删除,无法访问,或含糊不清的拷贝赋值运算符)
    Original:
    T has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous copy assignment operator)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T有直接或虚基类不能被复制分配(已删除,无法访问,或暧昧的举动赋值运算符)
    Original:
    T has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T有一个用户声明的移动构造函数
    Original:
    T has a user-declared move constructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T有一个用户声明的移动赋值运算符
    Original:
    T has a user-declared move assignment operator
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[编辑] 简单拷贝赋值运算符

隐式声明的拷贝赋值运算符类T以下是微不足道的,如果是真实的
Original:
The implicitly-declared copy assignment operator for class T is trivial if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T没有虚成员函数
    Original:
    T has no virtual member functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T没有虚基类
    Original:
    T has no virtual base classes
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 选择每一个直接基础T的拷贝赋值运算符是微不足道的
    Original:
    The copy assignment operator selected for every direct base of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 选择每一个非静态类类型(或类类型的数组)星期四会员T的拷贝赋值运算符是微不足道的
    Original:
    The copy assignment operator selected for every non-static class type (or array of class type) memeber of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
一个平凡的拷贝赋值运算符的一个副本对象表示,如果由std::memmove。所有兼容的数据类型与C语言(POD类型)简单复制分配.
Original:
A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 隐式定义拷贝赋值运算符

如果隐式声明的拷贝赋值运算符是不会被删除或微不足道的,它的定义,函数体生成和编译的编译器。 union类型,隐式定义拷贝赋值复制对象表示(std::memmove)的。对于非工会类类型(classstruct),操作员执行成员,明智的副本分配的对象的基础和非静​​态成员,在他们的初始化顺序,使用,使用内置的在分配的标量和拷贝赋值运算符为类的类型.
Original:
If the implicitly-declared copy assignment operator is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy assignment copies the object representation (as by std::memmove). For non-union class types (class and struct), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using, using built-in assignment for the scalars and copy assignment operator for class types.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
生成的隐式定义的拷贝赋值运算符为​​deprecated(C++11 起),如果T用户声明析构函数或用户声明的拷贝构造函数.
Original:
The generation of the implicitly-defined copy assignment operator is deprecated(C++11 起) if T has a user-declared destructor or user-declared copy constructor.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 注释

如果复制和移动赋值运算符重载解析选择移动分配的说法是“右值”(或者“prvalue”如一个无名的临时或“xvalue”的结果std::move ),并选择拷贝赋值的说法是“左值”(命名对象或函数/操作符返回左值参考)。如果仅仅是拷贝赋值,所有的参数类别选择(只要它采取它的参数的值或为const,因为右值绑定到const引用),这使得拷贝赋值移动分配的回退,当移动是不可用
Original:
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std::move), and selects the copy assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 复制和交换

拷贝赋值运算符的拷贝构造函数,析构函数和swap()成员函数可表​​示,如果一个人提供
Original:
Copy assignment operator can be expressed in terms of copy constructor, destructor, and the swap() member function, if one is provided:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

T& T::operator=(T arg) { // copy/move constructor is called to construct arg
    swap(arg);    // resources exchanged between *this and arg
    return *this;
}  // destructor is called to release the resources formerly held by *this

对于非投掷交换(),这种形式提供强异常保证。本表对于右值参数,自动调用移动的构造,有时也被称为“统一赋值运算符”(如,复制和移动).
Original:
For non-throwing swap(), this form provides 强异常保证. For rvalue arguments, this form automatically invokes the move constructor, and is sometimes referred to as "unifying assignment operator" (as in, both copy and move).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 为例

#include <iostream>
#include <memory>
struct A {
    int n;
    std::string s1;
    // user-defined copy assignment, copy-and-swap form
    A& operator=(A other) {
        std::cout << "copy assignment of A\n";
        std::swap(n, other.n);
        std::swap(s1, other.s1);
        return *this;
    }
};
 
struct B : A {
    std::string s2;
    // implicitly-defined copy assignment
};
 
struct C {
     std::unique_ptr<int[]> data;
     std::size_t size;
     // non-copy-and-swap assignment
     C& operator=(const C& other) {
         // check for self-assignment
         if(&other == this)
             return *this;
         // reuse storage when possible
         if(size != other.size)
             data.reset(new int[other.size]);
         std::copy(&other.data[0],
                   &other.data[0] + std::min(size, other.size),
                   &data[0]);
         return *this;
     }
     // note: copy-and-swap would always cause a reallocation
};
 
int main()
{
    A a1, a2;
    std::cout << "a1 = a2 calls ";
    a1 = a2; // user-defined copy assignment
 
    B b1, b2;
    b2.s1 = "foo";
    b2.s2 = "bar";
    std::cout << "b1 = b2 calls ";
    b1 = b2; // implicitly-defined copy assignment
    std::cout << "b1.s1 = " << b1.s1 << " b1.s2 = " << b1.s2 <<  '\n';
}

Output:

a1 = a2 calls copy assignment of A
b1 = b2 calls copy assignment of A
b1.s1 = foo b1.s2 = bar