Mickey Mathieson wrote:
> --- shvetakapoor2002_cplusplus
> <[EMAIL PROTECTED]> wrote:
> 
>> Hi All,
>>  
>> Please help me in understanding what's the
>> difference between the two 
>> statements below
>>  
>> [1] cout << "Number: " << static_cast<int>(3.14159)
>> << "\n";
>>
>> [2] cout << "Number: " << (int)3.14159 << "\n";
>>  
>> Basically I want to know the difference in using
>> static_cast<int> and 
>> using (int)
>>
>> Thanks,
>> Shveta
>>
>>
> 
> http://www.cprogramming.com/reference/typecasting/staticcast.html
> 
> 
> Mickey M.
> Construction Partner Inc.
> http://www.constructionpartner.com

Um...

double result = static_cast<double>(4)/5;

Pretty sure that won't work and should be:

double result = static_cast<double>(4)/static_cast<double>(5);

Or you could just skip the casting and just use doubles to begin with:

double result = 4.0 / 5.0;


Frankly, I've not seen much of a difference between the two types of 
casts and personally prefer the old C-style casts because it requires 
pressing fewer keys.  However, I'll use static_cast<>() when typecasting 
a (void *) to some other type - usually the (void *) is a pointer to a 
class that is being sent to a static private callback function within 
the class itself.  But for a simple "I need to convert from one basic 
type to another basic type", static_cast seems overkill.

IMO, cprogramming.com is a pretty unreliable site for answering C/C++ 
questions.  The C++ FAQ that gets referenced here often is significantly 
more accurate.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.0
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to