Default constructors
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
默认构造函数是一个构造函数可以调用任何参数(或者定义一个空的参数列表,或提供的每个参数的默认参数)。一个公共的默认构造函数是A型
DefaultConstructible
.原文:
A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is
DefaultConstructible
.目录 |
[编辑] 语法
ClassName ( ) ;
|
(1) | ||||||||
ClassName :: ClassName ( ) body
|
(2) | ||||||||
ClassName() = delete ;
|
(3) | (C++11 起) | |||||||
ClassName() = default ;
|
(4) | (C++11 起) | |||||||
[编辑] 解释
1)
声明一个默认的构造函数
2)
定义的构造函数在类体外
原文:
Definition of the constructor outside the class body
3)
抑制自动生成一个默认的构造函数
原文:
Inhibiting the automatic generation of a default constructor
4)
显式强制自动生成一个默认的构造函数
原文:
Explicitly forcing the automatic generation of a default constructor
ClassName
是封闭类的标识符原文:
ClassName
is the identifier of the enclosing class默认的构造函数被调用时
原文:
The default constructors are called when:
- 创建对象或数组是静态的,本地线程,并自动存储时间被宣布没有初始化,(T obj;或T arr[10];)原文:creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj; or T arr[10];)
- 动态存储时间创建对象时,新的表达在编写时没有一个初始(new T;)原文:creating objects of dynamic storage duration when the new-expression is written without an initializer (new T;)
- 创建阵列的的动态存储时间的表达new T[n]原文:creating arrays of dynamic storage duration with the expression new T[n]
- 创造价值的初始化临时对象与演员表达T().原文:creating value-initialized temporary objects with the cast expression T().
[编辑] 隐式声明默认构造函数
如果没有提供任何形式的用户定义的构造函数的类类型(struct,class,或union),编译器将始终作为
inline public
它的类成员声明一个默认的构造函数。如果一些用户定义的构造函数,用户可能会强制生成的隐式声明的构造函数的关键字default
(C++11 起).原文:
If no user-defined constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an
inline public
member of its class. If some user-defined constructors are present, the user may still force the generation of the implicitly declared constructor with the keyword default
(C++11 起).[编辑] 删除隐式声明的缺省构造函数
隐式声明的或默认的默认构造函数类
T
是不确定的(至 C++11)/定义为“删除”(C++11 起)如果以下是真实的原文:
The implicitly-declared or defaulted default constructor for class
T
is undefined (至 C++11) / defined as deleted (C++11 起) if any of the following is true:-
T
具有引用类型的成员(无大括号或等于initializer(C++11 起)).原文:T
has a member of reference type (without a brace-or-equal initializer(C++11 起)). -
T
有const成员(没有括号或等于initializer(C++11 起)的)或用户定义的默认构造函数.原文:T
has a const member (without a brace-or-equal initializer(C++11 起)) or a user-defined default constructor. -
T
有一个成员(没有括号或等于initializer(C++11 起)的),其中有一个删除默认的构造函数,其默认的构造函数是含糊不清或无法从这个构造函数.原文:T
has a member (without a brace-or-equal initializer(C++11 起)), which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor. -
T
有直接或虚基类,有删除默认的构造函数,或者是含糊不清或无法从这个构造函数.原文:T
has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor. -
T
直接或虚基类,它有一个删除析构函数或析构函数,此构造函数是无法从.原文:T
has a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor. -
T
与至少一种变体与非平凡的默认constructorunion的部件是一个(C++11 起).原文:T
is a union with at least one variant member with non-trivial default constructor(C++11 起). -
T
是一个union和它的变体成员都const.原文:T
is a union and all of its variant members are const.
[编辑] 常用的默认构造函数
隐式声明的默认构造函数类
T
以下是微不足道的,如果是真实的原文:
The implicitly-declared default constructor for class
T
is trivial if all of the following is true:-
T
没有虚成员函数 -
T
没有虚基类 -
T
没有括号或等于初始值设定项(C++11 起)的非静态成员原文:T
has no non-static members with brace-or-equal initializers (C++11 起) - 每一个直接基础
T
有一个简单的默认构造函数原文:Every direct base ofT
has a trivial default constructor - 每类类型的非静态成员常用的默认构造函数原文:Every non-static member of class type has a trivial default constructor
一个平凡的默认构造函数是一个构造函数不执行任何操作。与常用的默认构造函数的对象,可以创建reinterpret_cast的的任何存储使用,例如: std::malloc分配的内存。所有兼容的数据类型与C语言(POD类型)是平凡的默认构造的
原文:
A trivial default constructor is a constructor that performs no action. Objects with trivial default constructors can be created by using reinterpret_cast的 on any storage, e.g. on memory allocated with std::malloc. All data types compatible with the C language (POD types) are trivially default-constructible.
[编辑] 隐式定义的默认构造函数
如果隐式声明的默认构造函数是不会被删除或微不足道的,它的定义,函数体生成和编译的编译器,它有相同的效果作为一个用户定义的构造函数,空体和空初始化列表。也就是说,它会调用默认的构造函数的基地,这个类的非静态成员.
原文:
If the implicitly-declared default constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class.
[编辑] 示例
struct A { int x; A(int x = 1) : x(x) {} // user-defined default ctor }; struct B : A { // B::B() is implicitly-defined, calls A::A() }; struct C { A obj; // C::C() is implicitly-defined, calls A::A() }; struct D : A { D(int y) : A(y) {} // D::D() is not declared because another constructor exists }; struct E : A { E(int y) : A(y) {} E() = default; // explicitly defaulted, calls A::A() }; struct F { int& ref; // reference member const int c; // const member // Bad::Bad() is deleted }; int main() { A a; B b; // D d; // compile error E e; // F f; // compile error }