This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C++ question: where does int() come from ?
John (Eljay) Love-Jensen schrieb:
> Hi Michael,
>
>> Is the int('A') a function call provided by C++ library or the compiler will do the conversion during compilation time ?
>
> I might be getting the terminology wrong, but here goes anyway...
>
> The int('A') is a constructor cast. For me, I prefer it to the C style cast, ala (int)'A'. Especially in the kind of situation in your example.
>
> I'm not sure what the general consensus is amongst the C++ developer community regarding constructor casts, but I do know that there is a general disdain of C style casts. The recommendation by the C++ gurus is to prefer static_cast, const_cast, dynamic_cast, and reinterpret_cast ... and I presume that constructor casts would be right up there as well.
Actually it is not a "constructor cast", but a constructor call. In C++,
even the built-in types (such as int, double, ...) have a default- and a
copy-constructor.
int(<some integer expression>) invokes the copy-constructor for the int
type, resulting in a (temporary) int instance. The type conversion
("cast") is performed even before that:'A' is (by integer promotion)
implicitly extended to an int while being passed to the copy-constructor.
Daniel