Variable Initialization
在 C++ 中,创建变量的同时赋予初始值叫 初始化(Initialization)。
初始化类型
共有 6 种初始化方式:
Initialization Type | Example | Note |
---|---|---|
Default initialization | int x; |
Undefined behavior, x could be any value |
Copy initialization | int x = 5; |
|
Direct initialization | int x(5); |
|
Direct list initialization | int x{5}; |
Narrowing conversions disallowed |
Copy list initialization | int x = {5}; |
Narrowing conversions disallowed |
Value initialization | int x{}; |
Usually performs zero initialization |
未初始化变量
没有被 初始化 并且没有被 赋值(assignment) 的变量称为 未初始化变量(uninitialized variable)。
Initialization
When an object is defined, you can optionally give it an initial value. The process of specifying an initial value for an object is called initialization, and the syntax used to initialize an object is called an initializer.[1]