Type Casting in C and C++

Hello NG

As an old style programmer I've been casting values with the C-syntax like:

double dVal=3.14;
float fVal = (float) dVal;

but now I've been told that the C++ syntax should do a better job

float fVal = static_cast <float>(dVal);

for some reason. Is this true? Will the C++ compiler really generate 
different (better?) code or is it simply more writing I have to do?

Thanx
Richie 


0
5/3/2006 7:33:07 AM
vc.mfc 33608 articles. 0 followers. Follow

1 Replies
827 Views

Similar Articles

[PageSpeed] 35

>As an old style programmer I've been casting values with the C-syntax like:
>
>double dVal=3.14;
>float fVal = (float) dVal;
>
>but now I've been told that the C++ syntax should do a better job
>float fVal = static_cast <float>(dVal);
>
>for some reason. Is this true? Will the C++ compiler really generate 
>different (better?) code or is it simply more writing I have to do?

The compiler is unlikely to do anything different in that case, but it
is safer to use the new style casts for a variety of reasons
including:

1. static_cast is fairly mild in what you're able to do with it, the
compiler will moan if you're doing something naughty.
2. The new casts are quite specific in what they do.
3. You can search for them easier in source code.

Dave
0
davidl7375 (2060)
5/3/2006 2:10:32 PM
Reply:

Similar Artilces: