You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/initializers.md
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ Initializers may take these forms:
64
64
65
65
## Kinds of initialization
66
66
67
-
There are several kinds of initialization, which may occur at different points in program execution. Different kinds of initialization are not mutually exclusive—for example, list initialization can trigger value initialization and in other circumstances, it can trigger aggregate initialization.
67
+
There are several kinds of initialization, which may occur at different points in program execution. Different kinds of initialization aren't mutually exclusive—for example, list initialization can trigger value initialization and in other circumstances, it can trigger aggregate initialization.
68
68
69
69
### Zero initialization
70
70
@@ -114,27 +114,27 @@ MyClass mc1;
114
114
MyClass* mc3 = new MyClass;
115
115
```
116
116
117
-
If the class, struct, or union does not have a default constructor, the compiler emits an error.
117
+
If the class, struct, or union doesn't have a default constructor, the compiler emits an error.
118
118
119
-
Scalar variables are default initialized when they are defined with no initialization expression. They have indeterminate values.
119
+
Scalar variables are default initialized when they're defined with no initialization expression. They have indeterminate values.
120
120
121
121
```cpp
122
122
int i1;
123
123
float f;
124
124
char c;
125
125
```
126
126
127
-
Arrays are default initialized when they are defined with no initialization expression. When an array is default-initialized, its members are default initialized and have indeterminate values, as in the following example:
127
+
Arrays are default initialized when they're defined with no initialization expression. When an array is default-initialized, its members are default initialized and have indeterminate values, as in the following example:
128
128
129
129
```cpp
130
130
int int_arr[3];
131
131
```
132
132
133
-
If the array members do not have a default constructor, the compiler emits an error.
133
+
If the array members don't have a default constructor, the compiler emits an error.
134
134
135
135
#### Default initialization of constant variables
136
136
137
-
Constant variables must be declared together with an initializer. If they are scalar types they cause a compiler error, and if they are class types that have a default constructor they cause a warning:
137
+
Constant variables must be declared together with an initializer. If they're scalar types they cause a compiler error, and if they're class types that have a default constructor they cause a warning:
vector<int> v = 10; // the constructor is explicit; compiler error C2440: cannot convert from 'int' to 'std::vector<int,std::allocator<_Ty>>'
255
+
vector<int> v = 10; // the constructor is explicit; compiler error C2440: can't convert from 'int' to 'std::vector<int,std::allocator<_Ty>>'
256
256
regex r = "a.*b"; // the constructor is explicit; same error
257
257
shared_ptr<int> sp = newint(1729); // the constructor is explicit; same error
258
258
```
@@ -422,7 +422,7 @@ myArr3: 8 9 10 0 0
422
422
423
423
#### Initializing unions and structs
424
424
425
-
If a uniondoes not have a constructor, you can initialize it with a single value (or with another instance of a union). The value is used to initialize the first non-static field. This is different from struct initialization, in which the first value in the initializer is used to initialize the first field, the second to initialize the second field, and so on. Compare the initialization of unions and structs in the following example:
425
+
If a uniondoesn't have a constructor, you can initialize it with a single value (or with another instance of a union). The value is used to initialize the first non-static field. This is different from struct initialization, in which the first value in the initializer is used to initialize the first field, the second to initialize the second field, and so on. Compare the initialization of unions and structs in the following example:
426
426
427
427
```cpp
428
428
struct MyStruct {
@@ -488,7 +488,7 @@ int main()
488
488
}
489
489
```
490
490
491
-
The only way to initialize a reference with a temporary object is to initialize a constant temporary object. Once initialized, a reference-type variable always points to the same object; it cannot be modified to point to another object.
491
+
The only way to initialize a reference with a temporary object is to initialize a constant temporary object. Once initialized, a reference-type variable always points to the same object; it can't be modified to point to another object.
492
492
493
493
Although the syntax can be the same, initialization of reference-type variables and assignment to reference-type variables are semantically different. In the preceding example, the assignments that change `iVar` and `lVar` look similar to the initializations, but have different effects. The initialization specifies the object to which the reference-type variable points; the assignment assigns to the referred-to object through the reference.
494
494
@@ -527,10 +527,10 @@ The decision graph begins with: is the initializer an lvalue of the same type or
527
527
:::image-end:::
528
528
Decision graph for initialization of reference types
529
529
530
-
References to **`volatile`** types (declared as **`volatile`** *typename*<strong>&</strong> *identifier*) can be initialized with **`volatile`** objects of the same type or with objects that have not been declared as **`volatile`**. They cannot, however, be initialized with **`const`** objects of that type. Similarly, references to **`const`** types (declared as **`const`** *typename*<strong>&</strong> *identifier*) can be initialized with **`const`** objects of the same type (or anything that has a conversion to that type or with objects that have not been declared as **`const`**). They cannot, however, be initialized with **`volatile`** objects of that type.
530
+
References to **`volatile`** types (declared as **`volatile`** *typename*<strong>&</strong> *identifier*) can be initialized with **`volatile`** objects of the same type or with objects that haven't been declared as **`volatile`**. They can't, however, be initialized with **`const`** objects of that type. Similarly, references to **`const`** types (declared as **`const`** *typename*<strong>&</strong> *identifier*) can be initialized with **`const`** objects of the same type (or anything that has a conversion to that type or with objects that haven't been declared as **`const`**). They can't, however, be initialized with **`volatile`** objects of that type.
531
531
532
-
References that are not qualified with either the **`const`** or **`volatile`** keyword can be initialized only with objects declared as neither **`const`** nor **`volatile`**.
532
+
References that aren't qualified with either the **`const`** or **`volatile`** keyword can be initialized only with objects declared as neither **`const`** nor **`volatile`**.
533
533
534
534
### Initialization of external variables
535
535
536
-
Declarations of automatic, static, and external variables can contain initializers. However, declarations of external variables can contain initializers only if the variables are not declared as **`extern`**.
536
+
Declarations of automatic, static, and external variables can contain initializers. However, declarations of external variables can contain initializers only if the variables aren't declared as **`extern`**.
0 commit comments