Default 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. |
默认构造函数是一个构造函数可以调用任何参数(或者定义一个空的参数列表,或提供的每个参数的默认参数)。一个公共的默认构造函数是A型
DefaultConstructible.Original:
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.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.
目录 |
[编辑] 语法
ClassName ( ) ;
|
(1) | ||||||||
ClassName :: ClassName ( ) body
|
(2) | ||||||||
ClassName() = delete ;
|
(3) | (C++11 起) | |||||||
ClassName() = default ;
|
(4) | (C++11 起) | |||||||
[编辑] 解释
1)
声明一个默认的构造函数
Original:
Declaration of a 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.
2)
定义的构造函数在类体外
Original:
Definition of the constructor outside the class body
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.
3)
抑制自动生成一个默认的构造函数
Original:
Inhibiting the automatic generation of a 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.
4)
显式强制自动生成一个默认的构造函数
Original:
Explicitly forcing the automatic generation of a 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.
ClassName是封闭类的标识符Original:
ClassName is the identifier of the enclosing classThe 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 default constructors are called when:
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 obj;或T arr[10];)Original:creating objects or arrays of static, thread-local, and automatic storage duration that are declared without an initializer, (T obj; or T arr[10];)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 动态存储时间创建对象时,新的表达在编写时没有一个初始(new T;)Original:creating objects of dynamic storage duration when the new-expression is written without an initializer (new T;)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 创建阵列的的动态存储时间的表达new T[n]Original:creating arrays of dynamic storage duration with the expression new T[n]The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 创造价值的初始化临时对象与演员表达T().Original:creating value-initialized temporary objects with the cast expression T().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它的类成员声明一个默认的构造函数。如果一些用户定义的构造函数,用户可能会强制生成的隐式声明的构造函数的关键字default(C++11 起).Original:
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 起).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 default constructor for class
T is undefined (至 C++11) / defined as deleted (C++11 起) if 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具有引用类型的成员(无大括号或等于initializer(C++11 起)).Original:Thas a member of reference type (without a brace-or-equal initializer(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有const成员(没有括号或等于initializer(C++11 起)的)或用户定义的默认构造函数.Original:Thas a const member (without a brace-or-equal initializer(C++11 起)) or a user-defined default constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T有一个成员(没有括号或等于initializer(C++11 起)的),其中有一个删除默认的构造函数,其默认的构造函数是含糊不清或无法从这个构造函数.Original:Thas 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.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 a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.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 a direct or virtual base which has a deleted destructor, or a destructor that is inaccessible from this constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T与至少一种变体与非平凡的默认constructorunion的部件是一个(C++11 起).Original:Tis a union with at least one variant member with non-trivial default 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是一个union和它的变体成员都const.Original:Tis a union and all of its variant members are const.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 default 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没有括号或等于初始值设定项(C++11 起)的非静态成员Original:Thas no non-static members with brace-or-equal initializers (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:Every direct base ofThas a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 每类类型的非静态成员常用的默认构造函数Original:Every non-static member of class type has a trivial default constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
一个平凡的默认构造函数是一个构造函数不执行任何操作。与常用的默认构造函数的对象,可以创建reinterpret_cast的的任何存储使用,例如: std::malloc分配的内存。所有兼容的数据类型与C语言(POD类型)是平凡的默认构造的
Original:
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.
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:
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.
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 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 }

