Copy constructors
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
クラス
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.
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.
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.
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.
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.
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 typeT
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
の型がどこT
とf
void f(T t)ですOriginal:function argument passing: f(a);, wherea
is of typeT
andf
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(), wherea
is of typeT
, 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.
[編集]コピーコンストラクタが暗黙的に宣言された
は、ユーザー定義のコピーコンストラクタはクラス型(struct、class、または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.
You can help to correct and verify the translation. Click here for instructions.
T
のすべての直接的および仮想拠点はconstへのまたは彼らの最初のパラメータとして揮発性constへの参照を使用してコピーコンストラクタを持っていますOriginal:all direct and virtual bases ofT
have copy constructors with references to const or to const volatile as their first parametersThe 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 ofT
have copy constructors with references to const or to const volatile as their first parametersThe 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.
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.
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.
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 destructorThe 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.
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
のあらゆる直接ベース用に選択し、コピーコンストラクタは些細ですOriginal:The copy constructor selected for every direct base ofT
is trivialThe 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 ofT
is trivialThe 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.
You can help to correct and verify the translation. Click here for instructions.
[編集]コピーコンストラクタを暗黙的に定義された
暗黙的に宣言されたコピーコンストラクタが削除されたり、些細なことではないされている場合は、コンパイラによって(、関数本体が生成され、コンパイルされている)が定義されている。 unionタイプの場合、暗黙的に定義されたコピーコンストラクタは、オブジェクト表現を(std::memmoveするなど)がコピーされます。非組合クラスタイプ(classとstruct)では、コンストラクタは直接初期化を使用して、その初期化の順序で、オブジェクトの塩基および非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.
You can help to correct and verify the translation. Click here for instructions.
(C++11およびそれ以降)は、ユーザー定義のデストラクタまたはユーザー定義のコピー代入演算子を持っている場合、暗黙的に定義のコピーコンストラクタの生成はdeprecated
T
です.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.
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.
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}