what are the differences between this kind of initializing in c++;
int a = 0;
int a{};
int ();
why this code int a{3.14} get me error but this one int a = 3.14 or this one int a(3.14) do not
It's called list initialization (C++11) :
int foo = 0; // Initialize foo with 0
int foo{}; // Initialize foo with foo's type (int) default value, which is 0
int foo(); // Function declaration
int bar = 5.f; // Initialize bar with 5 (narrowing conversion from floating point)
int bar{5.f}; // Doesn't compile, because there is a loss of data when casting a float to an int
int bar(5.f); // Initialize bar with 5 (narrowing conversion from floating point)
However :
float f{5}; // Okay, because there is no loss of data when casting an int to a float
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With