Sign in to follow this  

DX10: Tutorial 10 type casting: why static_castfloat is used?

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. In Tutorial 10 static_cast operator appears only once: float fAspectRatio = static_cast<float>(pBufferSurfaceDesc->Width) / static_cast<float>(pBufferSurfaceDesc->Height); In the rest cases (float) type cast is used. Why we have to use static_cast operator in the line above? Is it safe to replace static_cast<float> with (float)?

Share this post


Link to post
Share on other sites
Older C-style casts are a bad thing for various reasons. I believe Scott Meyers included a great summary in one of his books.

Where possible you should use the C++ style casts - static_cast, dynamic_cast, const_cast and reinterpret_cast.

Quote:
Is it safe to replace static_cast<float> with (float)?
You're basically going from "definitely safe" to "probably safe" so you're basically making your code worse rather than better.

Although this is a trivial example, so it won't really make much difference. Go do some reading on C-style versus C++-style casting and you'll be a better programmer for it.


hth
Jack

Share this post


Link to post
Share on other sites
(float) casts are old style C casts. C++ provides several ways of casting, and one of them (appropriate here) is static_cast<float>. It's not a real problem to change between casting styles. The new style just makes sure you know what you're casting better when you write the code.

My guess is that that bit of code was added by another person than the one who wrote the rest.

Share this post


Link to post
Share on other sites
Inconsistent programming style?

The only reason for static_casts is to make casts more explicit compared to C-style casts (which do different things according to context in which they are used) and allow you to find it easier in code through find dialog, a very useful feature when you suspect there's something wrong with casts in your code.

So yes, you could safely replace it with C-style cast, and no, you shouldn't do that, in C++ it is considered a good practice to use C++ casts instead.


Edited: too late... need to learn to type faster.

Share this post


Link to post
Share on other sites

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this