Re: C++ question: where does int() come from ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux