value initialization
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
新しいオブジェクトにデフォルトの初期値を提供しています.
Original:
Provides the default initial value to a new 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.
目次 |
[編集]構文
Tobject{}; | (1) | (C++11およびそれ以降) | |||||||
T(); T | (2) | (C++11およびそれ以降) | |||||||
new T();
| (3) | (C++11およびそれ以降) | |||||||
[編集]説明
値の初期化は、次の3つの状況で実行されます
Original:
Value initialization is performed in three 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)
という名前の変数に、(自動、静的、またはスレッドローカル)括弧のペアで構成される初期化子を使用して宣言されたとき。 (C++11およびそれ以降)
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces. (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.
2)
無名の一時オブジェクトは括弧または中括弧の空の対からなる初期化子を使用して作成されたときに.
Original:
when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces.
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 an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces.
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 value 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
がどのような種類の少なくとも1ユーザーが提供するコンストラクタを持つクラス型である場合、デフォルトのコンストラクタですが呼び出されます.Original:IfT
is a class type with at least one user-provided constructor of any kind, the デフォルトのコンストラクタです is called.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
T
は、任意のユーザーが提供するコンストラクタなしで非組合クラス型である場合、そのオブジェクトはゼロで初期化れ、その後、暗黙的に宣言されたデフォルトのコンストラクタが(それは些細な場合を除きます)と呼ばれていますOriginal:IfT
is an non-union class type without any user-provided constructors, then the object is ゼロで初期化 and then the implicitly-declared default constructor is called (unless it's trivial)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
T
が配列型である場合は、配列の各要素の値は初期化されますOriginal:IfT
is an array type, each element of the array is value-initializedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- それ以外の場合は、オブジェクトはゼロで初期化され.Original:Otherwise, the object is zero-initialized.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集]ノート
構文T object();は、オブジェクトを初期化するのではなく、引数および戻り
T
を取らない関数を宣言します。ほとんどのコンパイラは、この場合にはコピーを最適化..:C + + 11の値が初期化してから、一時的なオブジェクトをコピーするには、初期化する、T object = T();た前という名前の変数を値に初期化する方法Original:
The syntax T object(); does not initialize an object; it declares a function that takes no arguments and returns
T
. The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case.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:
References cannot be value-initialized.
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.
シングルstd::vector引数を指定して場合やstd::listへの呼び出しによって成長し、構築時に、すべての標準的なコンテナ(
size_type
、resize()など)それらの要素を値初期化.Original:
All standard containers (std::vector, std::list, etc) value-initialize their elements when constructed with a single
size_type
argument or when grown by a call to resize().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 <vector>#include <iostream> struct T1 {int mem1;std::string mem2;};// no constructorsstruct T2 {int mem1;std::string mem2; T2(const T2&){}// a constructor, but no default};struct T3 {int mem1;std::string mem2; T3(){}// user-provided default ctor}; std::string s{};// calls default ctor, the value is "" (empty string)int main(){int n{};// non-class value-initialization, value is 0double f =double();// non-class value-init, value is 0.0int* a = new int[10]();// array of 10 zeroes T1 t1{};// no ctors: zero-initialized// t1.mem1 is zero-initialized// t1.mem2 is default-initialized// T2 t2{}; // error: has a ctor, but no default ctor T3 t3{};// user-defined default ctor:// t3.mem1 is default-initialized (the value is indeterminate)// t3.mem2 is default-initialized std::vector<int> v(3);// value-initializes three ints std::cout<< s.size()<<' '<< n <<' '<< f <<' '<< a[9]<<' '<< v[2]<<'\n';std::cout<< t1.mem1<<' '<< t3.mem1<<'\n'; delete[] a;}
出力:
0 0 0 0 0 0 4199376