Assignment operators
提供: cppreference.com
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
代入演算子はオブジェクトの値を変更.
Original:
Assignment operators modify the value of the object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
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
の内容を置き換えます。クラス型の場合、これはコピー代入演算子で述べる特別なメンバー関数、です.Original:
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 コピー代入演算子.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
移動代入演算子は、可能であれば(
a
が変更される場合があります)コピーを回避、b
の内容を持つオブジェクトb
の内容を置き換えます。クラス型の場合、これは代入演算子を移動しますで述べる特別なメンバ関数です。 (C++11およびそれ以降) Original:
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およびそれ以降) The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
非クラス型の場合は、コピーや移動の割り当ては、区別がつかなく、として直接割り当てと呼ばれ.
Original:
For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
複合代入演算子
a
とa
の値の前回値との間の二項演算の結果と内容をオブジェクトb
の内容を置き換える.Original:
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
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 組み込みの直接代入
あらゆるタイプ
T
、次の関数のシグネチャは、オーバーロードの解決に参加するOriginal:
For every type
T
, the following function signatures participate in overload resolution:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
T*& operator=(T*&, T*); |
||
T*volatile & operator=(T*volatile &, T*); |
||
すべての列挙型またはメンバー·タイプの
T
へのポインタの場合は、必要に応じてvolatile修飾子は、以下の関数のシグネチャは、オーバーロードの解決に参加していますOriginal:
For every enumeration or pointer to member type
T
, optionally volatile-qualified, the following function signature participates in overload resolution:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
T& operator=(T&, T ); |
||
A1は算術型(オプションでvolatile修飾)で、A2が昇格算術型であるすべてのペアA1とA2は、次の関数のシグネチャは、オーバーロードの解決に参加しています
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
A1& operator=(A1&, A2); |
||
任意のスカラー型の
T
のE1式の場合は、組み込みの代入式の次の追加フォームが許可されていますOriginal:
For expressions E1 of any scalar type
T
, the following additional forms of the builtin assignment expression are allowed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
E1 = {} |
(C++11およびそれ以降) | |
E1 = {E2} |
(C++11およびそれ以降) | |
注:上記の直接割り当てることはありません、参照型、配列型、関数型、および型voidを除き、すべての非クラスタイプが含まれています.
Original:
Note: the above includes all non-class types except reference types, array types, function types, and the type void, which are not directly assignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
直接代入演算子は、左オペランドとして変更可能な左辺値を予想し、修正後の左オペランドを識別する左辺値を返します。非クラス型の場合は、右のオペランドが左のオペランドのcv-修飾されていない型への最初の暗黙的に変換されますであり、その値は左側のオペランドによって識別されたオブジェクトにコピーされます.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
左オペランドが参照型である場合には、代入演算子は参照先のオブジェクトに適用されます.
Original:
When the left operand is a reference type, the assignment operator applies to the referred-to object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
左と右のオペランドがオブジェクトを識別する重複した場合、動作は未定義です(重複が正確であるとタイプが同じである場合を除く)
Original:
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)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
右のオペランドがブレース-initはリストである場合
Original:
If the right operand is a braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 表現E1 = {}はE1 = T()は
T
のタイプですE1
と同等です.Original:the expression E1 = {} is equivalent to E1 = T(), whereT
is the type ofE1
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 表現E1 = {E2}は暗黙の縮小変換することは禁止されていることを除いて、E1 = T(E2)は
T
のタイプですE1
と同等です.Original:the expression E1 = {E2} is equivalent to E1 = T(E2), whereT
is the type ofE1
, except that narrowing implicit conversions are prohibited.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス型の場合、この構文はstd::initializer_listの規則に従って、引数としてリスト初期化と代入演算子への呼び出しを生成します
Original:
For class types, this syntax generates a call to the assignment operator with std::initializer_list as the argument, following the rules of リスト初期化
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 例
このコードを実行します
#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は算術型(オプションでvolatile修飾)で、A2が昇格算術型であるすべてのペアA1とA2については、次の関数のシグネチャは、オーバーロードの解決に関与します
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
A1& operator*=(A1&, A2); |
||
A1& operator/=(A1&, A2); |
||
A1& operator+=(A1&, A2); |
||
A1& operator-=(A1&, A2); |
||
I1は整数型(オプションでvolatile修飾)で、I2は昇格整数型であるすべてのペアI1とI2については、次の関数のシグネチャは、オーバーロードの解決に関与します
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
I1& operator%=(I1&, I2); |
||
I1& operator<<=(I1&, I2); |
||
I1& operator>>=(I1&, I2); |
||
I1& operator&=(I1&, I2); |
||
I1& operator^=(I1&, I2); |
||
I1& operator|=(I1&, I2); |
||
すべてのオプションでのcv-修飾されたオブジェクト型の
T
、次の関数のシグネチャは、オーバーロードの解決に関与しますOriginal:
For every optionally cv-qualified object type
T
, the following function signatures participate in overload resolution:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
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}}}
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 例
This section is incomplete Reason: no example |
[編集] 参照
Common operators | ||||||
---|---|---|---|---|---|---|
代入 | incrementNJdecrement | 算術 | 論理 | 比較 | memberNJaccess | 他の |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast別の互換性のあるタイプ
に1型に変換します Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. dynamic_cast派生class
に仮想基底クラスに変換します Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. const_cast異なるcvqualifiers
と互換性のある型に型変換されます Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. reinterpret_cast互換性type
に型を変換します Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. new割り当てmemory
Original: new allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. delete割り当て解除memory
Original: delete deallocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeoftype
のサイズを照会します Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof...パラメーターパック(C++11およびそれ以降)
のサイズを照会します Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. typeidtype
の型情報を照会します Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. noexcept式が例外(C++11およびそれ以降)
を投げることができるかどうかをチェックします Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. alignofタイプ(C++11およびそれ以降)のクエリアラインメント要件を
Original: alignof queries alignment requirements of a type (C++11およびそれ以降) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |