Hi,
What is the difference between
andCode:double a = 4.5;
Both gives the same output withCode:double a(4.5);
Thanks.Code:cout << a << endl;
Printable View
Hi,
What is the difference between
andCode:double a = 4.5;
Both gives the same output withCode:double a(4.5);
Thanks.Code:cout << a << endl;
There is no difference. In C++, even though "a" is a simple double, for consistency it is advantageous for the syntax to initialize this variable to be similar to object initialization.
Note the similarities between the code above using the std::string class, and your code using the double.Code:#include <string>
int main()
{
std::string s1("abc123");
std::string s2 = "abc123";
}
Regards,
Paul McKenzie
Thanks.
Note that there may be a difference if the type is a class type rather than double, i.e., the former would not be allowed if the corresponding constructor was declared as explicit.