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/initializing-classes-and-structs-without-constructors-cpp.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
2
title: "Brace initialization for classes, structs, and unions"
3
-
description: "Use brace initialization with any C++ class, struct or union"
4
-
ms.date: "11/19/2019"
3
+
description: "Use brace initialization with any C++ class, struct, or union"
4
+
ms.date: 09/28/2021
5
5
ms.assetid: 3e55c3d6-1c6b-4084-b9e5-221b151402f4
6
6
---
7
7
# Brace initialization
8
8
9
-
It is not always necessary to define a constructor for a class, especially ones that are relatively simple. Users can initialize objects of a class or struct by using uniform initialization, as shown in the following example:
9
+
It isn't always necessary to define a constructor for a `class`, especially ones that are relatively simple. Users can initialize objects of a `class` or `struct` by using uniform initialization, as shown in the following example:
10
10
11
11
```cpp
12
12
// no_constructor.cpp
@@ -55,7 +55,7 @@ int main()
55
55
}
56
56
```
57
57
58
-
Note that when a class or struct has no constructor, you provide the list elements in the order that the members are declared in the class. If the class has a constructor, provide the elements in the order of the parameters. If a type has a default constructor, either implicitly or explicitly declared, you can use default brace initialization (with empty braces). For example, the following class may be initialized by using both default and non-default brace initialization:
58
+
When a `class` or `struct` has no constructor, you provide the list elements in the order that the members are declared in the `class`. If the `class` has a constructor, provide the elements in the order of the parameters. If a type has a default constructor, either implicitly or explicitly declared, you can use default brace initialization (with empty braces). For example, the following `class` may be initialized by using both default and non-default brace initialization:
59
59
60
60
```cpp
61
61
#include <string>
@@ -84,7 +84,7 @@ int main()
84
84
}
85
85
```
86
86
87
-
If a class has non-default constructors, the order in which class members appear in the brace initializer is the order in which the corresponding parameters appear in the constructor, not the order in which the members are declared (as with `class_a` in the previous example). Otherwise, if the type has no declared constructor, the order in which the members appear in the brace initializer is the same as the order in which they are declared; in this case, you can initialize as many of the public members as you wish, but you cannot skip any member. The following example shows the order that's used in brace initialization when there is no declared constructor:
87
+
If a class has non-default constructors, the order in which class members appear in the brace initializer is the order in which the corresponding parameters appear in the constructor, not the order in which the members are declared (as with `class_a` in the previous example). Otherwise, if the type has no declared constructor, member initializers must appear in the brace initializer in the same order as they're declared. In this case, you can initialize as many of the public members as you wish, but you can't skip any member. The following example shows the order that's used in brace initialization when there's no declared constructor:
88
88
89
89
```cpp
90
90
classclass_d {
@@ -106,7 +106,7 @@ int main()
106
106
}
107
107
```
108
108
109
-
If the default constructor is explicitly declared but marked as deleted, default brace initialization cannot be used:
109
+
If the default constructor is explicitly declared but marked as deleted, default brace initialization can't be used:
0 commit comments