Copy constructors
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
一个拷贝构造函数的类
T是一个非模板构造函数的第一个参数是T&,const T&,volatile T&,或const volatile T&,以及有没有其他的参数,其余的参数都具有默认值。 A型与公共拷贝构造函数是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.
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.
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.
- 初始化,T a = b;或T a(b);,其中b是类型
TOriginal:initialization, T a = b; or T a(b);, where b is of typeTThe 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);, whereais of typeTandfis 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(), whereais 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 ofThave 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 ofThave 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 起)在任何以下是真实的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:Thas 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:Thas 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:Thas 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:Thas 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:Tis 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:Thas 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:Thas 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:Thas 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 ofTis trivialThe 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 non-static class type (or array of class type) memeber ofTis 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)的。对于非工会类类型(classstruct),构造函数执行对象的基地和非静态成员的正式成员明智的副本,在他们的初始化顺序,使用直接初始化.
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.
隐式定义拷贝构造函数生成的为deprecated(C++11 起),如果
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 ctor volatile A va(10); // A a4 = va; // compile error C c; // C c2 = c; // compile error }

