value initialization
来自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. |
默认的初始值提供了一个新的对象.
Original:
Provides the default initial value to a new object.
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 object {};
|
(1) | (C++11 起) | |||||||
T();
T |
(2) | (C++11 起) | |||||||
new T ();
|
(3) | (C++11 起) | |||||||
[编辑] 解释
实惠中进行初始化的三种情况:
Original:
Value initialization is performed in three situations:
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.
1)
当一个命名的变量的初始值由一对大括号(自动,静态的,或本地线程)的声明。 (C++11 起)
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces. (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.
2)
一个无名的临时对象被创建时的初始值由一对空括号或大括号.
Original:
when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces.
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:
when an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces.
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.
初始化值的影响是:1
Original:
The effects of value initialization are:
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:IfTis a class type with at least one user-provided constructor of any kind, the 默认构造函数 is called.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
T是一个非工会没有任何用户提供的构造函数的类类型,那么该对象是零初始化,然后隐式声明的默认构造函数被调用(除非它是微不足道的)Original:IfTis an non-union class type without any user-provided constructors, then the object is 零初始化 and then the implicitly-declared default constructor is called (unless it's trivial)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
T如果是一个数组类型的数组,每个元素的值初始化Original:IfTis an array type, each element of the array is value-initializedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,对象是零初始化.Original:Otherwise, the object is zero-initialized.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
语法T object();不会初始化一个对象,它声明了一个函数,它没有参数和返回
T。价值的方式来命名的变量初始化C + +11之前T object = T();,其值初始化了一个临时的,然后复制初始化对象:在这种情况下,大多数编译器优化的副本.Original:
The syntax T object(); does not initialize an object; it declares a function that takes no arguments and returns
T. The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case.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:
References cannot be value-initialized.
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.
所有的标准容器(std::vector,std::list等)的值初始化
size_type一个单一的参数或调用resize()生长时,它们的元素构造时.Original:
All standard containers (std::vector, std::list, etc) value-initialize their elements when constructed with a single
size_type argument or when grown by a call to resize().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.
[编辑] 为例
#include <string> #include <vector> #include <iostream> struct T1 { int mem1; std::string mem2; }; // no constructors struct T2 { int mem1; std::string mem2; T2(const T2&) {} // a constructor, but no default }; struct T3 { int mem1; std::string mem2; T3() {} // user-provided default ctor }; std::string s{}; // calls default ctor, the value is "" (empty string) int main() { int n{}; // non-class value-initialization, value is 0 double f = double(); // non-class value-init, value is 0.0 int* a = new int[10](); // array of 10 zeroes T1 t1{}; // no ctors: zero-initialized // t1.mem1 is zero-initialized // t1.mem2 is default-initialized // T2 t2{}; // error: has a ctor, but no default ctor T3 t3{}; // user-defined default ctor: // t3.mem1 is default-initialized (the value is indeterminate) // t3.mem2 is default-initialized std::vector<int> v(3); // value-initializes three ints std::cout << s.size() << ' ' << n << ' ' << f << ' ' << a[9] << ' ' << v[2] << '\n'; std::cout << t1.mem1 << ' ' << t3.mem1 << '\n'; delete[] a; }
Output:
0 0 0 0 0 0 4199376