Default constructors
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
デフォルトコンストラクタは、引数(空のパラメータリストを持つ、または、パラメータごとに用意されているデフォルトの引数で定義された)なしで呼び出せるコンストラクタです。パブリックの既定コンストラクタを持つ型は
DefaultConstructible
です.Original:
A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is
DefaultConstructible
.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.
目次 |
[編集]構文
ClassName ( ) ; | (1) | ||||||||
ClassName :: ClassName ( ) body | (2) | ||||||||
ClassName() = delete ; | (3) | (C++11およびそれ以降) | |||||||
ClassName() = default ; | (4) | (C++11およびそれ以降) | |||||||
[編集]説明
1)
デフォルトコンストラクタの宣言
Original:
Declaration of a default constructor
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:
Definition of the constructor outside the class body
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:
Inhibiting the automatic generation of a default constructor
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:
Explicitly forcing the automatic generation of a default constructor
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.
ClassName
囲むクラスの識別子ですOriginal:
ClassName
is the identifier of the enclosing classThe 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 default constructors are called when:
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 obj;またはT arr[10];)Original:creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj; or T arr[10];)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 新しい式は初期化子(new T;)を使用せずに書き込まれたときに動的な記憶域期間のオブジェクトを作成するOriginal:creating objects of dynamic storage duration when the new-expression is written without an initializer (new T;)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 表現new T[n]持つ動的記憶期間の配列を作成しますOriginal:creating arrays of dynamic storage duration with the expression new T[n]The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - キャスト式のT()と値に初期化された一時オブジェクトを作成.Original:creating value-initialized temporary objects with the cast expression T().The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集]既定のコンストラクタは、暗黙的に宣言された
いかなる種類のユーザー定義コンストラクタはクラス型(struct、class、またはunion)のために提供されていない場合、コンパイラは常にそのクラスの
inline public
メンバーとしてデフォルトコンストラクタを宣言します。いくつかのユーザー定義のコンストラクタが存在する場合、ユーザーは依然としてキーワードで暗黙的に宣言されたコンストラクタを強制的に生成させることができるdefault
(C++11およびそれ以降).Original:
If no user-defined constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an
inline public
member of its class. If some user-defined constructors are present, the user may still force the generation of the implicitly declared constructor with the keyword default
(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.
[編集]暗黙的に宣言されたデフォルトのコンストラクタを削除しました
クラス
T
ための暗黙的に宣言された、またはデフォルト設定デフォルトのコンストラクタは未定義(C++11以前)/削除(C++11およびそれ以降)次のいずれかに該当する場合のように定義されますOriginal:
The implicitly-declared or defaulted default constructor for class
T
is undefined (C++11以前) / defined as deleted(C++11およびそれ以降) if any of the following is true: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
参照型(ブレースか等しいinitializer(C++11およびそれ以降)なし)のメンバーを持ってい.Original:T
has a member of reference type (without a brace-or-equal initializer(C++11およびそれ以降)).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
const部材(ブレースか等しいinitializer(C++11およびそれ以降)なし)またはユーザー定義のデフォルトコンストラクタを持っている.Original:T
has a const member (without a brace-or-equal initializer(C++11およびそれ以降)) or a user-defined default constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
削除された既定のコンストラクターを持つか、そのデフォルトコンストラクタこのコンストラクタからあいまいであるか、またはアクセス不能である部材(ブレースか等しいinitializer(C++11およびそれ以降)なし)があり、.Original:T
has a member (without a brace-or-equal initializer(C++11およびそれ以降)), which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
削除デフォルトコンストラクタを持つ、直接または仮想基盤を持っているか、それは、このコンストラクタからあいまいであるか、またはアクセスできません.Original:T
has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
削除デストラクタ、このコンストラクタからアクセスできないデストラクタを持つ、直接または仮想基盤を持って.Original:T
has a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
非自明なデフォルトconstructorunionと少なくとも1つの変異部材と(C++11およびそれ以降)です.Original:T
is a union with at least one variant member with non-trivial default constructor(C++11およびそれ以降).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
unionであり、その変異体のすべてのメンバーがconstです.Original:T
is a union and all of its variant members are const.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集]些細なデフォルトコンストラクタ
以下のすべての条件が真である場合、クラス
T
ための暗黙的に宣言された既定のコンストラクタは、些細です:Original:
The implicitly-declared default constructor for class
T
is trivial if all of the following is true: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:T
has no virtual member functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
ない仮想基底クラスを持っていませんOriginal:T
has no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
ブレースか等しい初期(C++11およびそれ以降)との非静的メンバを持っていませんOriginal:T
has no non-static members with brace-or-equal initializers (C++11およびそれ以降)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
のすべての直接のベースは些細なデフォルトコンストラクタを持っていますOriginal:Every direct base ofT
has a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- クラス型のすべての非静的メンバは些細なデフォルトコンストラクタを持っていますOriginal:Every non-static member of class type has a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
些細な既定のコンストラクターは、アクションを実行しませんコンストラクタです。自明なデフォルトコンストラクタを持つオブジェクトは、例えば、任意のストレージ上reinterpret_castはを使用して作成することができますメモリ上std::mallocで割り当て。 C言語(POD型)と互換性のあるすべてのデータ型は自明デフォルトコンストラクト可能です.
Original:
A trivial default constructor is a constructor that performs no action. Objects with trivial default constructors can be created by using reinterpret_castは on any storage, e.g. on memory allocated with std::malloc. All data types compatible with the C language (POD types) are trivially default-constructible.
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 implicitly-declared default constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class.
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.
[編集]例
struct A {int x; A(int x =1): x(x){}// user-defined default ctor};struct B : A {// B::B() is implicitly-defined, calls A::A()};struct C { A obj;// C::C() is implicitly-defined, calls A::A()};struct D : A { D(int y): A(y){}// D::D() is not declared because another constructor exists};struct E : A { E(int y): A(y){} E()=default;// explicitly defaulted, calls A::A()}; struct F {int& ref;// reference memberconstint c;// const member// Bad::Bad() is deleted}; int main(){ A a; B b;// D d; // compile error E e;// F f; // compile error}