copy initialization
提供: cppreference.com
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
別のオブジェクトからオブジェクトを初期化します
Original:
Initializes an object from another 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.
目次 |
[編集] 構文
T object = other ;
|
(1) | ||||||||
f( other) ;
|
(2) | ||||||||
return other;
|
(3) | ||||||||
catch ( T other) ;
|
(4) | ||||||||
T array [ N ] = { other };
|
(5) | ||||||||
[編集] 説明
コピーの初期化は、次のような状況で行われます
Original:
Copy initialization is performed in the following situations:
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.
1)
という名前の変数が(自動、静的、またはスレッドローカル)で構成された初期化子を使用して宣言されたときの表現が続く等号.
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
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.
2)
値で関数に引数を渡すとき
Original:
when passing an argument to a function by value
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.
3)
値で返す関数から戻ったとき
Original:
when returning from a function that returns by value
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.
4)
値で例外をキャッチしたとき
Original:
when catching an exception by value
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.
5)
集約の初期化の一環として、初期化子が提供される各要素を初期化する
Original:
as part of 集約の初期化, to initialize each element for which an initializer is provided
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:
The effects of copy initialization are:
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
は、クラス型とother、T
のコンストラクタはT
またはT
から派生したクラスのcv-修飾されていないバージョンが検査されており、最高の試合がオーバーロード解決により選択されたタイプである場合。コンストラクタは、そのオブジェクトを初期化するために呼び出されます.Original:IfT
is a class type and the type of other is cv-unqualified version ofT
or a class derived fromT
, the constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
T
がクラス型であり、otherの型が異なる場合、またはT
は非クラス型ですが、otherの型はクラス型で、ユーザー定義の変換シーケンスにotherの型から変換できるT
が検査され、最高である場合1は、オーバーロードの解決によって選択されます。変換先の型の一時prvalueです変換、の結果はその後直接初期化しますオブジェクトに使用されます。最後のステップは、通常排除で、変換関数の結果は、対象のオブジェクトに割り当てられたメモリに直接構築されますが、コピーコンストラクタは、それが使用されていないにもかかわらず、アクセス可能であることが要求される.Original:IfT
is a class type, and the type of other is different, or ifT
is non-class type, but the type of other is a class type, ユーザー定義の変換シーケンス that can convert from the type of other toT
are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to 直接初期化します the object. The last step is usually 排除 and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- それ以外の場合(
T
もotherのタイプどちらもクラス型である場合)、標準の変換はotherのcv-修飾されていないバージョンにT
の値を変換するために、必要であれば、使用されている.Original:Otherwise (if neitherT
nor the type of other are class types), 標準の変換 are used, if necessary, to convert the value of other to the cv-unqualified version ofT
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集] ノート
コピー初期化は、直接初期未満寛大です:コピー初期化唯一の非明示的なコンストラクタやユーザ定義の変換関数を考慮.
Original:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
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.
otherは右辺値式である場合、コンストラクタに移動しますは、オーバーロードの解決によって選択とコピー、初期化時に呼び出されます。.
Original:
If other is an rvalue expression, コンストラクタに移動します will be selected by overload resolution and called during copy-initialization.
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:
The equals sign,
=
, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.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 <string> #include <utility> #include <memory> int main() { std::string s = "test"; // OK: constructor is non-explicit std::string s2 = std::move(s); // this copy-initialization performs a move // std::unique_ptr<int> p = new int(1); // error: constructor is explicit std::unique_ptr<int> p(new int(1)); // OK: direct-initialization int n = 3.14; // floating-integral conversion const int b = n; // const doesn't matter int c = b; // ...either way }