zero initialization
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
设置一个对象的初始值为零
原文:
Sets the initial value of an object to zero
目录 |
[编辑] 语法
static T object ;
|
(1) | ||||||||
int () ;
|
(2) | ||||||||
char array [ n ] = "";
|
(3) | ||||||||
[编辑] 解释
零初始化是在以下几种情况:
原文:
Zero initialization is performed in the following situations:
1)
对于每一个已命名的变量与静态或线程局部存储的持续时间,在任何其他初始化.
原文:
For every named variable with static or thread-local storage duration, before any other initialization.
2)
3)
当一个字符数组初始化一个字符串文字太短,其余是零初始化的数组.
原文:
When a character array is initialized with a string literal that is too short, the remainder of the array is zero-initialized.
零初始化的效果是:
原文:
The effects of zero initialization are:
-
T
是一个非工会的类类型,所有的基类和非静态数据成员是零初始化,初始化所有的填充零位。构造函数,如果有的话,被忽视原文:IfT
is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.
-
T
是一个联合类型,第一个非静态的数据成员是零初始化,初始化所有的填充零位.原文:IfT
is a union type, the first non-static named data member is zero-initialized and all padding is initialized to zero bits.
-
T
是数组类型,每个元素都是零初始化原文:IfT
is array type, each element is zero-initialized
- 如果
T
是引用类型,什么都不做.原文:IfT
is reference type, nothing is done.
[编辑] 注释
零初始化的静态和线程局部变量是第一,然后重新初始化程序中指定,例如:函数局部静态是第一个在程序启动时初始化为零的,那么它的构造函数被调用该函数时,第一次进入。如果一个类的静态声明没有初始化,那么默认的初始化什么也不做,把结果较早的零初始化未修改的.
原文:
The static and thread-local variables are first zero-initialized and then initialized again as specified in the program, e.g. a function-local static is first zero-initialized at program startup, and then its constructor is called when the function is first entered. If the declaration of a non-class static has no initializer, then default initialization does nothing, leaving the result of the earlier zero-initialization unmodified.
初始化为零的指针是空指针值,即使它的类型的空指针的值不是整数零.
原文:
A zero-initialized pointer is the null pointer value of its type, even if the value of the null pointer is not integral zero.
[编辑] 示例
#include <string> double f[3]; // zero-initialized to three 0.0's int* p; // zero-initialized to null pointer value std::string s; // zero-initialized to indeterminate value // then default-initialized to "" int main(int argc, char* argv[]) { static int n = argc; // zero-initialized to 0 // then copy-initialized to argc delete p; // safe to delete a null pointer }