Initializing multiple variables in C++
Initializing multiple variables In the last section, we noted that it is possible to define multiple variables of the same type in a single statement by separating the names with a comma: int a , b ; COPY We also noted that best practice is to avoid this syntax altogether. However, since you may encounter other code that uses this style, it’s still useful to talk a little bit more about it, if for no other reason than to reinforce some of the reasons you should be avoiding it. You can initialize multiple variables defined on the same line: int a = 5 , b = 6 ; // copy initialization int c ( 7 ) , d ( 8 ) ; // direct initialization int e { 9 } , f { 10 } ; // brace initialization (preferred) COPY Unfortunately, there’s a common pitfall here that can occur when the programmer mistakenly tries to initialize both variables by using one initialization statement: int a , b = 5 ; // wrong (a is not initialized!) int a = 5 , b = 5 ; // correct...