名前空間
変種
操作

Default constructors

提供: 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.
インラインアセンブリ
 
デフォルトコンストラクタは、引数(空のパラメータリストを持つ、または、パラメータごとに用意されているデフォルトの引数で定義された)なしで呼び出せるコンストラクタです。パブリックの既定コンストラクタを持つ型は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.

目次

[編集] 構文

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.
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.
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.
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.
ClassName囲むクラスの識別子です
Original:
ClassName is the identifier of the enclosing class
The text has been machine-translated via Google Translate.
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.
  • 初期化せずに宣言された静的スレッドローカル、および自動記憶域期間のオブジェクトや配列を作成し、(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.

[編集] 既定のコンストラクタは、暗黙的に宣言された

いかなる種類のユーザー定義コンストラクタはクラス型(structclass、または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.

[編集] 暗黙的に宣言されたデフォルトのコンストラクタを削除しました

クラス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.
  • 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.
  • Tconst部材(ブレースか等しい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.
  • Tunionであり、その変異体のすべてのメンバーが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.
  • Tない仮想メンバ関数を持っていません
    Original:
    T has no virtual member functions
    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 no virtual base classes
    The 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 of T has a trivial default constructor
    The 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 constructor
    The 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.

[編集] デフォルトコンストラクタを暗黙的に定義された

暗黙的に宣言されたデフォルトのコンストラクタが削除されたり、些細なことではないされている場合は、コンパイラによって(、関数本体が生成され、コンパイルされている)を定義して、それは空の体と空を持つユーザー定義のコンストラクタとまったく同じ効果を持っています初期化子リスト。すなわち、それは基地の、このクラスの非静的メンバのデフォルトコンストラクタを呼び出し.
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.

[編集]

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 member
    const int c; // const member
    // Bad::Bad() is deleted
};
 
int main()
{
    A a;
    B b;
//  D d; // compile error
    E e;
//  F f; // compile error
}