
David A. answered 10/10/19
Experienced C++ tutor who loves to mentor and pass on my knowledge
Using braces {} in this way is part of Uniform Initialization. Let's take a look using the code below:
Output
x: 100
y: 100
ar[0]: 1
ar2[0]: 1
ar[1]: 2
ar2[1]: 2
ar[2]: 3
ar2[2]: 3
ar[3]: 4
ar2[3]: 4
ar[4]: 5
ar2[4]: 5
Uniform initialization is a feature in C++ 11 that allows the usage of a consistent syntax to initialize variables and objects ranging from primitive type to aggregates. In other words, it introduces brace-initialization that uses braces ({}) to enclose initializer values.
Objects can be initialized with both the equal sign (=) and braces ({}); however, assignment (not initialization) also uses the equal sign (=), therefore, supporting braced initialization maintains a uniformity for initialization as opposed to assignment.
Morale: Even though braces and equal signs are supported for initialization...
- For initialization, use {}
- For assignment, use =