Possible Duplicates:
C++ cast syntax styles
What is the difference between (type)value and type(value) ?
In C++, when explicitly converting one built-in type to another, you can write:
int x = (int)c;
int x = int(c);
int d = (double)f;
int d = double(f);
I known that (T)v is a C-style cast, and I think the other syntax isn’t technically a cast, but what is the other syntax called and what are its semantics? (And where to use which?)
T(value)is actually an initialization of the typeT, and because it’s an initialization, an implicit type conversion can take place if the type ofvalueandTare convertible. IfTis a class-object, then one of it’s constructors is called, either a default constructor that takes a single value andTandvalueare implicitly convertible types, or the copy constructor with the same condition that the two types are implicitly convertible.(T)value, as you’ve noted, is a C-style cast from the type ofvalue, to the typeT. Both in the end though are pretty much doing the same thing under the hood since if you didyou’d get the exact same result, that is both create/return an object of type
Twhich an be used to initialize an l-value of typeT.