Jump to content
  • Advertisement
Sign in to follow this  
projectghost

C++ Type-casting, but not really

This topic is 3156 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

Ok maybe not the best title to describe this issue (and it is actually a trivial one) so here it goes: After reading somewhere on a the programmer portfolio review thread here on Gamedev.net some people mentioned that using lines of code like, float f; int i = (int)f; is not very C++ like but more C like and that they should use int i = int(f); int i = static_cast<int>(f); So what do you think and what version do you use? [Edited by - projectghost on February 26, 2010 12:33:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
In the cases you provided, everything is basically the same, so it comes down to a style issue. There is thus no one "right" answer.

Share this post


Link to post
Share on other sites
I don't use the C++ versions (except the const_cast) because:
-static casts are too long to write;
-I don't use reinterpret_casts, dynamic casts and RTTI.
-const_cast is an exception because it may point to possible bugs whenever I decide to work with things like that.
I'm not really a professional now but I'm developing a practical coding style for myself.

Share this post


Link to post
Share on other sites
Quote:
Original post by projectghost
I was more wondering what was actually used in professional application/games
The worst one ;P

Share this post


Link to post
Share on other sites
Quote:
Original post by snake5
-static casts are too long to write;


IMHO, that is not a good argument. Plus using static_cast allows me to search or search and replace far easier then C style cast. And that itself it's worth its weight in gold.

Share this post


Link to post
Share on other sites
Quote:
Original post by snake5
There's no right and there's no worst. They use what works best for them.
Right and wrong doesn't exclude the possibility of a reasonable middle ground.

On a less philosophical note, the presence of casts (particularly dynamic_cast) in your code generally indicates that you screwed up the design phase. The exception is when you have to interface with a legacy APIs, which are often not const-correct, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by DoctorGlow
Quote:
Original post by snake5
-static casts are too long to write;


IMHO, that is not a good argument. Plus using static_cast allows me to search or search and replace far easier then C style cast. And that itself it's worth its weight in gold.


Exactly my thoughts. If static casts are too long to write, maybe C++ programming is not for you? Because there are a lot of stuff in C++ that is too long to write, maybe don't write at all - like with static casts.

Quote:
Original post by snake5
-I don't use reinterpret_casts, dynamic casts and RTTI.

I'm pretty sure you use reinterpret casts, but you doing them with C syntax.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!