Copy assignment operator
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
一个拷贝赋值运算符的类
T
是一个非模板的非静态成员函数的名称operator=,它只有一个参数的类型T,T&,const T&,volatile T&,或const volatile T&。与公共拷贝赋值运算符是A型CopyAssignable
.原文:
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
.目录 |
[编辑] 语法
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可以使用#典型的拷贝赋值运算符的声明
原文:
# Typical declaration of a copy assignment operator when copy-and-swap idiom can be used
#典型的拷贝赋值运算符时不能使用复制和交换成语声明
原文:
# Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used
#强制编译器生成的一个拷贝赋值运算符
原文:
# Forcing a copy assignment operator to be generated by the compiler
#避免隐式的拷贝赋值
选择重载决议,例如拷贝赋值运算符时,被称为一个对象时,会出现在赋值表达式的左侧.
原文:
The copy assignment operator is called whenever selected by 重载决议, e.g. when an object appears on the left side of an assignment expression.
[编辑] 隐式声明的拷贝赋值运算符
如果没有用户定义的复制赋值运算符的类类型(struct,class,或union),编译器将声明一个内联类的成员。这隐式声明的拷贝赋值运算符的形式T& T::operator=(const T&)以下是真实的
原文:
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:
- 每个直接基
B
的T
有一个拷贝赋值运算符的参数是B
或const B&
const volatile B&原文:each direct baseB
ofT
has a copy assignment operator whose parameters areB
orconst B&
or const volatile B& - 每个非静态数据成员
M
T
类类型或类类型的数组有一个拷贝赋值运算符的参数是M
或const M&
const volatile M&原文:each non-static data memberM
ofT
of class type or array of class type has a copy assignment operator whose parameters areM
orconst M&
or const volatile M&
否则隐式声明的拷贝赋值运算符被声明为T& T::operator=(T&)。 (请注意,由于这些规则,隐式声明的拷贝赋值运算符不能绑定到动荡的左值参数)
原文:
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)
一个类可以有多个拷贝赋值运算符,如: T& T::operator=(const T&)和T& T::operator=(T)。如果一些用户自定义的拷贝赋值运算符都存在,用户仍然可以强制生成的隐式声明的拷贝赋值运算符与关键字
default
.原文:
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
.因为拷贝赋值运算符的任何类,基类的赋值运算符总是宣称永远是隐藏的。如果一个声明是用来把从基类中的赋值运算符,它的参数类型可以是相同的参数类型的隐式派生类的赋值运算符,声明还隐藏着由隐声明.
原文:
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.
[编辑] 删除隐式声明的拷贝赋值运算符
隐式声明的或默认的拷贝赋值运算符类
T
被定义为“删除”以下是正确的:原文:
The implicitly-declared or defaulted copy assignment operator for class
T
is defined as deleted in any of the following is true:-
T
有一个非静态数据成员是const原文:T
has a non-static data member that is const -
T
有一个非静态数据成员的引用类型.原文:T
has a non-static data member of a reference type. -
T
有一个非静态数据成员不能复制分配(已删除,无法访问,或含糊不清的拷贝赋值运算符)原文:T
has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous copy assignment operator) -
T
有直接或虚基类不能被复制分配(已删除,无法访问,或暧昧的举动赋值运算符)原文:T
has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator) -
T
有一个用户声明的移动构造函数原文:T
has a user-declared move constructor -
T
有一个用户声明的移动赋值运算符原文:T
has a user-declared move assignment operator
[编辑] 简单拷贝赋值运算符
隐式声明的拷贝赋值运算符类
T
以下是微不足道的,如果是真实的原文:
The implicitly-declared copy assignment operator for class
T
is trivial if all of the following is true:-
T
没有虚成员函数 -
T
没有虚基类 - 选择每一个直接基础
T
的拷贝赋值运算符是微不足道的原文:The copy assignment operator selected for every direct base ofT
is trivial - 选择每一个非静态类类型(或类类型的数组)星期四会员
T
的拷贝赋值运算符是微不足道的原文:The copy assignment operator selected for every non-static class type (or array of class type) memeber ofT
is trivial
一个平凡的拷贝赋值运算符的一个副本对象表示,如果由std::memmove。所有兼容的数据类型与C语言(POD类型)简单复制分配.
原文:
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.
[编辑] 隐式定义拷贝赋值运算符
如果隐式声明的拷贝赋值运算符是不会被删除或微不足道的,它的定义,函数体生成和编译的编译器。 union类型,隐式定义拷贝赋值复制对象表示(std::memmove)的。对于非工会类类型(class和struct),操作员执行成员,明智的副本分配的对象的基础和非静态成员,在他们的初始化顺序,使用,使用内置的在分配的标量和拷贝赋值运算符为类的类型.
原文:
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.
生成的隐式定义的拷贝赋值运算符为deprecated(C++11 起),如果
T
用户声明析构函数或用户声明的拷贝构造函数.原文:
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.[编辑] 注释
如果复制和移动赋值运算符重载解析选择移动分配的说法是“右值”(或者“prvalue”如一个无名的临时或“xvalue”的结果std::move ),并选择拷贝赋值的说法是“左值”(命名对象或函数/操作符返回左值参考)。如果仅仅是拷贝赋值,所有的参数类别选择(只要它采取它的参数的值或为const,因为右值绑定到const引用),这使得拷贝赋值移动分配的回退,当移动是不可用
原文:
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.
[编辑] 复制和交换
拷贝赋值运算符的拷贝构造函数,析构函数和swap()成员函数可表示,如果一个人提供
原文:
Copy assignment operator can be expressed in terms of copy constructor, destructor, and the swap() member function, if one is provided:
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
对于非投掷交换(),这种形式提供强异常保证。本表对于右值参数,自动调用移动的构造,有时也被称为“统一赋值运算符”(如,复制和移动).
原文:
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).
[编辑] 示例
#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'; }
输出:
a1 = a2 calls copy assignment of A b1 = b2 calls copy assignment of A b1.s1 = foo b1.s2 = bar