value initialization
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
默认的初始值提供了一个新的对象.
原文:
Provides the default initial value to a new object.
目录 |
[编辑] 语法
T object {};
|
(1) | (C++11 起) | |||||||
T();
T |
(2) | (C++11 起) | |||||||
new T ();
|
(3) | (C++11 起) | |||||||
[编辑] 解释
实惠中进行初始化的三种情况:
原文:
Value initialization is performed in three situations:
1)
当一个命名的变量的初始值由一对大括号(自动,静态的,或本地线程)的声明。 (C++11 起)
原文:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces. (C++11 起)
2)
一个无名的临时对象被创建时的初始值由一对空括号或大括号.
原文:
when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces.
3)
当一个对象被创建动态存储时间的初始值由一对空括号或大括号组成的一个新的表达.
原文:
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.
初始化值的影响是:1
原文:
The effects of value initialization are:
-
T
如果是一个数组类型的数组,每个元素的值初始化原文:IfT
is an array type, each element of the array is value-initialized
- 否则,对象是零初始化.原文:Otherwise, the object is zero-initialized.
[编辑] 注释
语法T object();不会初始化一个对象,它声明了一个函数,它没有参数和返回
T
。价值的方式来命名的变量初始化C + +11之前T object = T();,其值初始化了一个临时的,然后复制初始化对象:在这种情况下,大多数编译器优化的副本.原文:
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.引用不能是值初始化.
原文:
References cannot be value-initialized.
所有的标准容器(std::vector,std::list等)的值初始化
size_type
一个单一的参数或调用resize()生长时,它们的元素构造时.原文:
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().[编辑] 示例
#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; }
输出:
0 0 0 0 0 0 4199376