Assignment operators
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
赋值运算符的值修改的对象.
原文:
Assignment operators modify the value of the object.
| Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
|---|---|---|---|---|
| Inside class definition | Outside class definition | |||
| basic assignment | a = b
|
Yes | T& T::operator =(const T2& b); | N/A |
| move assignment (C++11) | a = rvalue
|
Yes | T& T::operator =(T2&& b); | N/A |
| addition assignment | a += b
|
Yes | T& T::operator +=(const T2& b); | T& operator +=(T& a, const T2& b); |
| subtraction assignment | a -= b
|
Yes | T& T::operator -=(const T2& b); | T& operator -=(T& a, const T2& b); |
| multiplication assignment | a *= b
|
Yes | T& T::operator *=(const T2& b); | T& operator *=(T& a, const T2& b); |
| division assignment | a /= b
|
Yes | T& T::operator /=(const T2& b); | T& operator /=(T& a, const T2& b); |
| modulo assignment | a %= b
|
Yes | T& T::operator %=(const T2& b); | T& operator %=(T& a, const T2& b); |
| bitwise AND assignment | a &= b
|
Yes | T& T::operator &=(const T2& b); | T& operator &=(T& a, const T2& b); |
| bitwise OR assignment | a |= b
|
Yes | T& T::operator |=(const T2& b); | T& operator |=(T& a, const T2& b); |
| bitwise XOR assignment | a ^= b
|
Yes | T& T::operator ^=(const T2& b); | T& operator ^=(T& a, const T2& b); |
| bitwise left shift assignment | a <<= b
|
Yes | T& T::operator <<=(const T2& b); | T& operator <<=(T& a, const T2& b); |
| bitwise right shift assignment | a >>= b
|
Yes | T& T::operator >>=(const T2& b); | T& operator >>=(T& a, const T2& b); |
| ||||
目录 |
[编辑] 解释
“”拷贝赋值运算符的内容替换的对象
a的副本b(b没有修改的内容)。对于类类型,这是一个特殊的成员函数,在复制赋值操作者.原文:
copy assignment operator replaces the contents of the object
a with a copy of the contents of b (b is no modified). For class types, this is a special member function, described in 复制赋值操作者.'移动分配“运算符的内容替换对象
ab的内容,避免如果可能的复制(b可以修改)。对于类类型,这是一个特殊的成员函数,在移动赋值运算符。 (C++11 起) 原文:
move assignment operator replaces the contents of the object
a with the contents of b, avoiding copying if possible (b may be modified). For class types, this is a special member function, described in 移动赋值运算符. (C++11 起) 对于非类类型,复制和移动分配是无法区分的,被称为“直接分配”.
原文:
For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment.
“复合赋值”运营商的内容替换对象
a的内容的二进制运算的结果之间a的先前的值和值b.原文:
compound assignment operators replace the contents the contents of the object
a with the result of a binary operation between the previous value of a and the value of b.[编辑] 内置的直接分配
对于每一个类型
T,下面的函数签名参与重载解析原文:
For every type
T, the following function signatures participate in overload resolution:| T*& operator=(T*&, T*); |
||
| T*volatile & operator=(T*volatile &, T*); |
||
对于每一个枚举成员的指针类型
T,选择性挥发合格,下面的函数签名参与重载决议原文:
For every enumeration or pointer to member type
T, optionally volatile-qualified, the following function signature participates in overload resolution:| T& operator=(T&, T ); |
||
每对A1和A2,其中A1是一个算术类型(可选volatile限定)和A2是一个促进的算术类型,下面的函数签名参与重载决议
原文:
For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:
| A1& operator=(A1&, A2); |
||
对于任何标量类型
T的表达式E1,下列额外的内置的赋值表达式是允许的:原文:
For expressions E1 of any scalar type
T, the following additional forms of the builtin assignment expression are allowed:| E1 = {} |
(C++11 起) | |
| E1 = {E2} |
(C++11 起) | |
注:以上包括所有非类类型的引用类型,数组类型,函数类型,类型void,这是不直接分配的除外.
原文:
Note: the above includes all non-class types except reference types, array types, function types, and the type void, which are not directly assignable.
直接赋值运算符预期修改的左值作为左操作数,并返回一个左值,确定修改后的左操作数。对于非类类型,右操作数是隐式转换的cv不合格的左操作数的类型,那么它的价值被复制到左操作数确定的对象.
原文:
The direct assignment operator expects a modifiable lvalue as its left operand and returns an lvalue identifying the left operand after modification. For non-class types, the right operand is first 隐式转换 to the cv-unqualified type of the left operand, and then its value is copied into the object identified by left operand.
当左操作数是一个引用类型,赋值运算符的参考对象.
原文:
When the left operand is a reference type, the assignment operator applies to the referred-to object.
如果左侧和右侧的操作数识别重叠的对象,则该行为是未定义的(除非该重叠是精确的并且该类型是相同的)
原文:
If the left and the right operands identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same)
如果右操作数是一个支撑初始化列表
原文:
If the right operand is a braced-init-list
- 表达E1 = {}相当于E1 = T(),其中
T是类型E1.原文:the expression E1 = {} is equivalent to E1 = T(), whereTis the type ofE1. - 表达E1 = {E2}是相当于E1 = T(E2)
T是E1类型,其中,除了缩小隐式转换被禁止.原文:the expression E1 = {E2} is equivalent to E1 = T(E2), whereTis the type ofE1, except that narrowing implicit conversions are prohibited.
类的类型,语法生成调用赋值操作符与std::initializer_list作为参数,列表初始化的规则
原文:
For class types, this syntax generates a call to the assignment operator with std::initializer_list as the argument, following the rules of 列表初始化
[编辑] 示例
#include <iostream> int main() { int n = 0; // not an assignment n = 1; // direct asignment std::cout << n << ' '; n = {}; // zero-initialization, then assignment std::cout << n << ' '; n = 'a'; // integral promotion, then assignment std::cout << n << ' '; n = {'b'}; // explicit cast, then assignment std::cout << n << ' '; n = 1.0; // floating-point conversion, then assignment std::cout << n << ' '; // n = {1.0}; // compiler error (narrowing conversion) int& r = n; // not an assignment int* p; r = 2; // assignment through reference std::cout << n << ' '; p = &n; // direct assignment p = nullptr; // null-pointer conversion, then assignment }
输出:
1 0 97 98 1 2
[编辑] 内置复合赋值
每对A1和A2,A1是一个算术类型(可选volatile限定)和A2是一个促进的算术类型,下面的函数签名参与重载解析
原文:
For every pair A1 and A2, where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signatures participate in overload resolution:
| A1& operator*=(A1&, A2); |
||
| A1& operator/=(A1&, A2); |
||
| A1& operator+=(A1&, A2); |
||
| A1& operator-=(A1&, A2); |
||
对于每一个对I1和I2,I1是一个不可分割的类型(可选volatile限定)和I2是促进整体式,下面的函数签名参与重载决策
原文:
For every pair I1 and I2, where I1 is an integral type (optionally volatile-qualified) and I2 is a promoted integral type, the following function signatures participate in overload resolution:
| I1& operator%=(I1&, I2); |
||
| I1& operator<<=(I1&, I2); |
||
| I1& operator>>=(I1&, I2); |
||
| I1& operator&=(I1&, I2); |
||
| I1& operator^=(I1&, I2); |
||
| I1& operator|=(I1&, I2); |
||
对于每一个可选品种合格的对象类型
T,下面的函数签名参与重载解析原文:
For every optionally cv-qualified object type
T, the following function signatures participate in overload resolution:| T*& operator+=(T*&, std::ptrdiff_t); |
||
| T*& operator-=(T*&, std::ptrdiff_t); |
||
| T*volatile & operator+=(T*volatile &, std::ptrdiff_t); |
||
| T*volatile & operator-=(T*volatile &, std::ptrdiff_t); |
||
{{{1}}}
[编辑] 示例
| 本章尚未完成 原因:暂无示例 |
[编辑] 另请参阅
| Common operators | ||||||
|---|---|---|---|---|---|---|
| 分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
|
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
| Special operators | ||||||