C++ Type-casting, but not really
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]
Quote:Original post by Dragonsoulj
Check out this page.
I was more wondering what was actually used in professional application/games
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.
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.
-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.
Quote:Original post by projectghostThe worst one ;P
I was more wondering what was actually used in professional application/games
Quote:The worst one ;P
There's no right and there's no worst. They use what works best for them.
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.
Quote:Original post by snake5Right and wrong doesn't exclude the possibility of a reasonable middle ground.
There's no right and there's no worst. They use what works best for them.
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.
Quote:Original post by DoctorGlowQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement