(int() function vs (int) cast - comments please
-
Sometimes, it is necessary to change a floating point number to an integer. The usual way of doing this is just to write
i = (int)f;
However, the C++ compiler also lets one express this desire like this:i = int( f );
which is how functions are normally written, and it is easier to understand in complicated expressions rather than casts. Similar expressions can be produced with other implicit types eg double and float egdbl = double( i );
Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? All comments are welcome:) -
Sometimes, it is necessary to change a floating point number to an integer. The usual way of doing this is just to write
i = (int)f;
However, the C++ compiler also lets one express this desire like this:i = int( f );
which is how functions are normally written, and it is easier to understand in complicated expressions rather than casts. Similar expressions can be produced with other implicit types eg double and float egdbl = double( i );
Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? All comments are welcome:)i had a look at this in the disassembler, didnt find any difference! Don't try it, just do it! ;-)
-
Sometimes, it is necessary to change a floating point number to an integer. The usual way of doing this is just to write
i = (int)f;
However, the C++ compiler also lets one express this desire like this:i = int( f );
which is how functions are normally written, and it is easier to understand in complicated expressions rather than casts. Similar expressions can be produced with other implicit types eg double and float egdbl = double( i );
Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? All comments are welcome:)FearlessBurner wrote:
_i = (int)f;_
Is a cast. FearlessBurner wrote:_i = int( f );_
Constructs an unnamed (temporary) int, using f as the initialiser value, and assigns/copies the value to i. Your compiler probably optimises this. FearlessBurner wrote:_dbl = double( i );_
Is similar to the previous comment, and constructs a temporary unnamed double. FearlessBurner wrote: Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? It probably became possible to write this when you changed from C to C++. My own recommendation is to cast using the C++ casts, instead of the C casts, so for the example above I'd write:i = static_cast<int> (f);
Because it's easier to find casts that use the C++ syntax for casts over the C syntax, and using the correct C++ cast can reduce or highlight potential errors caused by casting - this particularly applies to the other three C++ casts (const_cast
,dynamic_cast
, andreinterpret_cast
) -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky