名前空間
変種
操作

Copy 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.
デフォルトのコンストラクタです
コンストラクタをコピーしてください
コンストラクタに移動します (C++11)
テンプレート
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.
インラインアセンブリ
 
クラスTのコピーコンストラクタは、すべてのデフォルト値を持っている最初のパラメータT&const T&volatile T&、またはconst volatile T&であり、どちらかの他のパラメータが存在しない、非テンプレートコンストラクタ、またはパラメータの残りです。公共のコピーコンストラクタを持つ型はCopyConstructibleです.
Original:
A copy constructor of class T is a non-template constructor whose first parameter is T&, const T&, volatile T&, or const volatile T&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public copy constructor is CopyConstructible.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 構文

class_name ( const class_name & ) (1)
class_name ( const class_name & ) = default; (1)
class_name ( const class_name & ) = delete; (1)

[編集] 説明

コピーコンストラクタの第典型的な宣言
Original:
# Typical declaration of a copy constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#コピーコンストラクタがコンパイラによって生成されるように強制する
Original:
# Forcing a copy constructor to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#暗黙のデフォルトコンストラクタを回避
Original:
# Avoiding implicit 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:
The copy constructor is called whenever an object is initialized from another object of the same type, which includes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • bはタイプT a = b;ある初期化、またはT a(b);T
    Original:
    initialization, T a = b; or T a(b);, where b is of type T
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 関数の引数の受け渡し:f(a);aの型がどこTfvoid f(T t)です
    Original:
    function argument passing: f(a);, where a is of type T and f is void f(T t)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 関数の戻り値:return a;T f()はなくムーブコンストラクタを持たない型a、あるTなどの関数内の.
    Original:
    function return: return a; inside a function such as T f(), where a is of type T, which has no move constructor.
    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メンバーとしてコピーコンストラクタを宣言します。次のすべてに該当する場合は、この暗黙的に宣言されたコピーコンストラクタは、フォームT::T(const T&)があります
Original:
If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as an inline public member of its class. This implicitly-declared copy constructor has the form T::T(const T&) 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のすべての直接的および仮想拠点はconstへのまたは彼らの最初のパラメータとして揮発性constへの参照を使用してコピーコンストラクタを持っています
    Original:
    all direct and virtual bases of T have copy constructors with references to const or to const volatile as their first parameters
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Tのすべての非静的メンバはconstへのまたは彼らの最初のパラメータとして揮発性constへの参照を使用してコピーコンストラクタを持っています
    Original:
    all non-static members of T have copy constructors with references to const or to const volatile as their first parameters
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
そうでない場合は、暗黙的に宣言されたコピーコンストラクタはT::T(T&)です。 (これらの規則のために、暗黙的に宣言されたコピーコンストラクタは揮発性左辺値引数にバインドできないことに注意してください)​​
Original:
Otherwise, the implicitly-declared copy constructor is T::T(T&). (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラスには、例えば、複数のコピーコンストラクタを持つことができ、 T::T(const T&)T::T(T&)両方。いくつかのユーザー定義のコピーコンストラクタが存在する場合、ユーザーは依然としてキーワードで暗黙的に宣言されたコピーコンストラクタを強制的に生成させることができるdefault(C + + 11以来).
Original:
A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy 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以来)次のいずれかではTRUEになります
Original:
The implicitly-declared or defaulted copy constructor for class T is undefined (C + + 11時まで) / defined as deleted (C + + 11以来) in 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(削除された、アクセスできない、またはあいまいなコピーコンストラクタ)にコピーすることはできません非静的データメンバを持っています
    Original:
    T has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors)
    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 direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors)
    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 direct or virtual base class with a deleted or inaccessible destructor
    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 a user-defined move constructor or move assignment operator (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以来)持つバリアント部材を有する
    Original:
    T is a union and has a variant member with non-trivial copy 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右辺値参照型(C + + 11以来)のデータメンバを持っています
    Original:
    T has a data member of rvalue reference 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.

[編集] ささいなコピーコンストラクタ

以下のすべての条件が真である場合、クラスTための暗黙的に宣言されたコピーコンストラクタは些細です:
Original:
The implicitly-declared copy 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のあらゆる直接ベース用に選択し、コピーコンストラクタは些細です
    Original:
    The copy constructor selected for every direct base of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Tのmemeberすべての非静的クラス型(またはクラス型の配列)のために選択、コピーコンストラクタは些細です
    Original:
    The copy constructor selected for every non-static class type (or array of class type) memeber of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
ささいなコピーコンストラクタは引数のオブジェクト表現のバイト単位のコピーを作成するコンストラクタであり、他のアクションを実行しません。ささいなコピーコンストラクタを持つオブジェクトは、例えば、手動でそれらのオブジェクト表現をコピーして、コピーすることができますstd::memmove持つ。 C言語(POD型)と互換性のあるすべてのデータ·タイプは自明コピー可能です.
Original:
A trivial copy constructor is a constructor that creates a bytewise copy of the object representation of the argument, and performs no other action. Objects with trivial copy constructors can be copied by copying their object representations manually, e.g. with std::memmove. All data types compatible with the C language (POD types) are trivially copyable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] コピーコンストラクタを暗黙的に定義された

暗黙的に宣言されたコピーコンストラクタが削除されたり、些細なことではないされている場合は、コンパイラによって(、関数本体が生成され、コンパイルされている)が定義されている。 unionタイプの場合、暗黙的に定義されたコピーコンストラクタは、オブジェクト表現を(std::memmoveするなど)がコピーされます。非組合クラスタイプ(classstruct)では、コンストラクタは直接初期化を使用して、その初期化の順序で、オブジェクトの塩基および非staticメンバの完全なメンバー単位のコピーを実行します.
Original:
If the implicitly-declared copy constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C + + 11以来)は、ユーザー定義のデストラクタまたはユーザー定義のコピー代入演算子を持っている場合、暗黙的に定義のコピーコンストラクタの生成はdeprecatedTです.
Original:
The generation of the implicitly-defined copy constructor is deprecated(C + + 11以来) if T has a user-defined destructor or user-defined copy assignment operator.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] ノート

彼らは観察可能な副作用を引き起こす場合でも、多くの状況では、コピーコンストラクタがタイムアウトに最適化され、省略をコピーしてくださいを参照してください
Original:
In many situations, copy constructors are optimized out even if they would produce observable side-effects, see 省略をコピーしてください
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 n;
    A(int n=1) : n(n) {}
    A(const A& a) : n(a.n) {} // user-defined copy ctor
};
 
struct B : A {
    // implicit default ctor B::B()
    // implicit copy ctor B::B(const B&) 
};
 
struct C : B {
     C() : B() {}
 private:
     C(const C&); // non-copiable, C++98 style
};
 
int main()
{
    A a1(7);
    A a2(a1); // calls the copy ctor
    B b;
    B b2 = b;
    A a3 = b; // conversion to A& and copy ctor
    volatile A va(10);
    // A a4 = va; // compile error
 
    C c;
    // C c2 = c; // compile error
}