The Wayback Machine - https://web.archive.org/web/20180316144627/http://ja.cppreference.com:80/w/cpp/language/copy_constructor
名前空間
変種
操作

Copy constructors

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定(廃止予定)
noexcept 指定子(C++11)
例外
名前空間
指定子
decltype(C++11)
auto(C++11)
alignas(C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr(C++11)
ユーザ定義(C++11)
ユーティリティ
アトリビュート(C++11)
typedef 宣言
型エイリアス宣言(C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
クラスTのコピーコンストラクタは、すべてのデフォルト値を持っている最初のパラメータT&const T&volatile T&、またはconstvolatile T&であり、どちらかの他のパラメータが存在しない、非テンプレートコンストラクタ、またはパラメータの残りです。公共のコピーコンストラクタを持つ型はCopyConstructibleです.
Original:
A copy constructor of class T is a non-template constructor whose first parameter is T&, const T&, volatile T&, or constvolatile 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 ( constclass_name& ) (1)
class_name ( constclass_name& ) = default; (1)
class_name ( constclass_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 ctorvolatile A va(10);// A a4 = va; // compile error   C c;// C c2 = c; // compile error}
close