c++11 - Variable initialization guidelines in C++ -
which guidelines initialize variables in c++? there many ways initialize variable in c++. 3 ways familiar following:
1) int a=3;
2) int a(3);
3) int a{3}; // valid c++11 & later
which best approach? why 2nd way int a(3); not seen & not used in c++ programs?
thanks.
the first 2 initializations have same effect. guess people prefer first option on second 1 because looks more natural.
the third option has different rules: forbids narrowing conversion. while can do
int = .3;
and int
equals zero,
int {.3};
will compile time error. brace initializer has advantage stops doing such conversions on accident , forces explicitly state intend if really want narrowing conversion.
Comments
Post a Comment