名前空間
変種
操作

Assignment operators

提供: cppreference.com
< cpp‎ | language

 
 
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.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
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.
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)
静的
特殊なメンバ関数
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.
テンプレート
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.
インラインアセンブリ
 
代入演算子はオブジェクトの値を変更.
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.
Operator name Syntax Over​load​able 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);
'注意'
Original:
Notes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • すべての組み込みの代入演算子は*thisを返し、ほとんどのユーザ定義のオーバーロードは、ユーザー定義演算子は組み込みと同様に使用することができる。*this戻すただし、ユーザー定義演算子のオーバーロードでは、任意の型、戻り値の型(void含む)として使用することができます.
    Original:
    All built-in assignment operators return *this, and most user-defined overloads also return *this so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2T含む任意の型を指定できます
    Original:
    T2 can be any type including T
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 説明

'コピー代入演算子はabない変更された)の内容のコピーを使用してオブジェクト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.
移動代入演算子は、可能であれば(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.
非クラス型の場合は、コピーや移動の割り当ては、区別がつかなく、として直接割り当てと呼ばれ.
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.
複合代入演算子aaの値の前回値との間の二項演算の結果と内容をオブジェクト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.

[編集] 組み込みの直接代入

あらゆるタイプ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.
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.
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.
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.
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.
直接代入演算子は、左オペランドとして変更可能な左辺値を予想し、修正後の左オペランドを識別する左辺値を返します。非クラス型の場合は、右のオペランドが左のオペランドの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.
左オペランドが参照型である場合には、代入演算子は参照先のオブジェクトに適用されます.
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.
左と右のオペランドがオブジェクトを識別する重複した場合、動作は未定義です(重複が正確であるとタイプが同じである場合を除く)
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.
右のオペランドがブレース-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.
  • 表現E1 = {}E1 = T()TのタイプですE1と同等です.
    Original:
    the expression E1 = {} is equivalent to E1 = T(), where T is the type of E1.
    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), where T is the type of E1, 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.

[編集]

#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 
}

Output:

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.
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.
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.
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.

[編集]

[編集] も参照してください

演算子の優先順位

Common operators
代入 incrementNJdecrement 算術 論理 比較 memberNJaccess 他の

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) 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:
const_cast converts type to compatible type with different cv qualifiers
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:
sizeof... queries the size of a パラメーターパック (C + + 11以来)
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:
noexcept checks if an expression can throw an exception (C + + 11以来)
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.