Copy constructors
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
一个拷贝构造函数的类
T
是一个非模板构造函数的第一个参数是T&,const T&,volatile T&,或const volatile T&,以及有没有其他的参数,其余的参数都具有默认值。 A型与公共拷贝构造函数是CopyConstructible
.原文:
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
.目录 |
[编辑] 语法
class_name ( const class_name & )
|
(1) | ||||||||
class_name ( const class_name & ) = default;
|
(1) | ||||||||
class_name ( const class_name & ) = delete;
|
(1) | ||||||||
[编辑] 解释
#典型的一个拷贝构造函数的声明
原文:
# Typical declaration of a copy constructor
#强制一个拷贝构造函数被编译器生成的
原文:
# Forcing a copy constructor to be generated by the compiler
#避免隐含的默认构造函数
原文:
# Avoiding implicit default constructor
从另一个相同类型的对象,其中包括一个对象初始化时被调用拷贝构造函数
原文:
The copy constructor is called whenever an object is initialized from another object of the same type, which includes
- 初始化,T a = b;或T a(b);,其中b是类型
T
原文:initialization, T a = b; or T a(b);, where b is of typeT
- 函数参数传递:f(a);,
a
的类型是T
和f
是void f(T t)原文:function argument passing: f(a);, wherea
is of typeT
andf
is void f(T t) - 函数返回:return a;T f(),
a
类型T
,如没有采取任何构造函数一个函数内部.原文:function return: return a; inside a function such as T f(), wherea
is of typeT
, which has no move constructor.
[编辑] 隐式声明的拷贝构造函数
如果没有用户定义的复制构造函数一类(struct,class,或union),编译器将始终作为
inline public
它的类成员的声明拷贝构造函数。隐式声明的拷贝构造函数的形式T::T(const T&)
以下是真实的原文:
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:- 所有直接和虚拟基础
T
的拷贝构造函数的引用为const或作为其第一个参数为const波动原文:all direct and virtual bases ofT
have copy constructors with references to const or to const volatile as their first parameters - 所有非静态成员
T
的拷贝构造函数的引用为const或作为其第一个参数为const波动原文:all non-static members ofT
have copy constructors with references to const or to const volatile as their first parameters
否则,隐式声明的拷贝构造函数是T::T(T&)。 (请注意,由于这些规则,隐式声明的拷贝构造函数不能绑定到动荡的左值参数)
原文:
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)
一个类可以有多个拷贝构造函数,例如T::T(const T&)和T::T(T&)。如果一些用户自定义拷贝构造函数存在,用户可能会强制生成的隐式声明的拷贝构造函数的关键字
default
(C++11 起).原文:
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 起).[编辑] 删除隐式声明的拷贝构造函数
隐式声明的或默认的拷贝构造函数的类
T
是不确定的(至 C++11)/定义为“删除”(C++11 起)在任何以下是真实的原文:
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:-
T
非静态数据成员不能被复制(已删除,无法访问,或含糊不清的拷贝构造函数)原文:T
has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors) -
T
有直接或虚基类,它不能被复制(已删除,无法访问,或含糊不清的拷贝构造函数)原文:T
has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors) -
T
有直接或虚基类被删除或无法访问的析构函数原文:T
has direct or virtual base class with a deleted or inaccessible destructor -
T
有一个用户定义的移动构造函数或移动赋值运算符(C++11 起)原文:T
has a user-defined move constructor or move assignment operator (C++11 起) -
T
是工会,有一个变体成员与非平凡的拷贝构造函数(C++11 起)原文:T
is a union and has a variant member with non-trivial copy constructor (C++11 起) -
T
右值引用类型的数据成员(C++11 起)原文:T
has a data member of rvalue reference type (C++11 起)
[编辑] 琐碎的拷贝构造函数
隐式声明的拷贝构造函数的类
T
以下是微不足道的,如果是真实的原文:
The implicitly-declared copy constructor for class
T
is trivial if all of the following is true:-
T
没有虚成员函数 -
T
没有虚基类 - 选择每一个直接基础
T
的拷贝构造函数是微不足道的原文:The copy constructor selected for every direct base ofT
is trivial - 选择每一个非静态类类型(或类类型的数组)星期四会员
T
的拷贝构造函数是微不足道的原文:The copy constructor selected for every non-static class type (or array of class type) memeber ofT
is trivial
一个平凡的拷贝构造函数是一个构造函数,创建一个基于字节的对象表示的参数的副本,并执行任何其他操作。与琐碎的拷贝构造函数的对象可以被复制的手动复制对象表示,如std::memmove。平凡复制的所有数据类型兼容的C语言(POD种类).
原文:
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.
[编辑] 隐式定义拷贝构造函数
如果隐式声明的拷贝构造函数是不会被删除或微不足道的,它的定义,函数体生成和编译的编译器。 union类型,隐式定义拷贝构造函数拷贝对象表示(std::memmove)的。对于非工会类类型(classstruct),构造函数执行对象的基地和非静态成员的正式成员明智的副本,在他们的初始化顺序,使用直接初始化.
原文:
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.
隐式定义拷贝构造函数生成的为deprecated(C++11 起),如果
T
有一个用户定义的析构函数或用户定义的拷贝赋值运算符.原文:
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.[编辑] 注释
在许多情况下,复制构造函数进行了优化,即使他们能观察到的副作用,副本省略
原文:
In many situations, copy constructors are optimized out even if they would produce observable side-effects, see 副本省略
[编辑] 示例
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 }