Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2002
Re: conversion from double to int


Message 1 in thread

Christopher Thompson wrote:
> 
> On Thursday 28 February 2002 12:38 pm, Omar wrote:
> > For something as simple as a double and integer
> > you could also just use the old C method
> > double a;
> > int b;
> >
> > b = (int) a;
> >
> > the static cast stuff is more useful for classes and pointers to
> > classes...makes it more robust
> 
> This is absolutely correct but note that the old C-style casts such as (int)
> are deprecated in C++.  Use static_cast or dynamic_cast (or one of the other
> two) for more safety.

And if those don't do it, there is the fallback equivalent to a C-style
cast, reinterpret_cast<>, which may not be a perfect match to C-style
functionality, but it is very close (static_cast is better if it works,
sometimes void pointers in network or other code requires
reinterpret_cast).

D. Stimits, stimits@idcomm.com

> 
> --
> List archive and information: http://qt-interest.trolltech.com


Message 2 in thread

> On Thursday 28 February 2002 9:49 am, Roderick Gors wrote:
> > hey,
> >
> > when using the function ceil() it returns a double, how can that be
> > converted to an int?? Roderick
> 
> Have you considered the standard C++ way:
> 
> static_cast<int>(doubleValue)
> 

What's wrong with just (int)doubleValue or int(doubleValue)? That's what
I've always used.

Nick


Message 3 in thread

> > On Thursday 28 February 2002 9:49 am, Roderick Gors wrote:
> > > hey,
> > >
> > > when using the function ceil() it returns a double, how can that be
> > > converted to an int?? Roderick
> >
> > Have you considered the standard C++ way:
> >
> > static_cast<int>(doubleValue)
> >
>
> What's wrong with just (int)doubleValue or int(doubleValue)? That's what
> I've always used.
>
> Nick
>
Me too, static_cast<int> is a bugger to type, but there are some good
arguments for using it:
C++ casts are easy to see, and easy to grep for in your source code and they
can easily be distinguished from things like:

some_class::operator some_type()

--
 [ signature omitted ] 

Message 4 in thread

What exactly does the static_cast do, anyway? Will it round the same way 
(int) does? Will it simply be an (int) for the compiler?


Paul Robertson wrote:

>>>On Thursday 28 February 2002 9:49 am, Roderick Gors wrote:
>>>
>>>>hey,
>>>>
>>>>when using the function ceil() it returns a double, how can that be
>>>>converted to an int?? Roderick
>>>>
>>>Have you considered the standard C++ way:
>>>
>>>static_cast<int>(doubleValue)
>>>
>>What's wrong with just (int)doubleValue or int(doubleValue)? That's what
>>I've always used.
>>
>>Nick
>>
>Me too, static_cast<int> is a bugger to type, but there are some good
>arguments for using it:
>C++ casts are easy to see, and easy to grep for in your source code and they
>can easily be distinguished from things like:
>
>some_class::operator some_type()
>
>--
>Paul
>
>--
>List archive and information: http://qt-interest.trolltech.com
>
>



Message 5 in thread

On Friday 01 March 2002 5:59 am, Nick Whitelegg wrote:
> > On Thursday 28 February 2002 9:49 am, Roderick Gors wrote:
> > > hey,
> > >
> > > when using the function ceil() it returns a double, how can that be
> > > converted to an int?? Roderick
> >
> > Have you considered the standard C++ way:
> >
> > static_cast<int>(doubleValue)
>
> What's wrong with just (int)doubleValue or int(doubleValue)? That's what
> I've always used.

From p. 131 of The C++ Programming Language, Special Edition, by Stroustrup:

"From C, C++ inherited the notation (T) e, which performs any conversion that 
can be expressed as a combination of static_casts, reinterpret_casts, and 
const_casts to make a value of type T from the expression e.  This C-style 
cast is far more dangerous than the named conversion operators because the 
notation is harder to spot in a large program and the kind of conversion 
intended by the programmer is not explicit.  That is, (T) e might be doing a 
portable conversion between related types, a nonportable conversion between 
unrelated types, or removing the const modifier from a pointer type.  Without 
knowing the exact types of T and e, you cannot tell."