Jump to content
  • Advertisement
Sign in to follow this  
dodheim

static_cast - why?

This topic is 4518 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! I can't figure out why static_cast are better than old c-style casts. I understand why dynamic_cast is great, but I don't see the purpose of static_cast. For ex. void* ptr; ... (1) char* pCh = (char*)ptr; (2) char* pCh = static_cast<char*>(ptr); thx, d0d

Share this post


Link to post
Share on other sites
Advertisement
I think in your case reinterpret_cast<char*>(ptr) is correct one (because you need to interpret pointer differently - reinterpret ;)

Share this post


Link to post
Share on other sites
Some benefits of c++ casts are: (Taken from Stroustrup's 'the C++ programming language')
- a static_cast enables the compiler to do at least some type checking, as this is for conversion between related types.
- having reinterpret_casts enables the programmer to quickly see where dangerous conversions are being made.
- C-style casts are a combination of: static_cast, reinterpret_cast and const_cast. So this is more dangerous because you can't easily recognize which kind of cast is intended.

Maybe worth mentioning too that according to Stroustrup explicit conversions are rarely needed in proper C++ programs.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The new cast-operators are more difficult to write, so you have two think twice before using one of them and they're easier to find (for humans or a search).

Share this post


Link to post
Share on other sites
Because it is more visible, better conveys the intent of the code, as well as catch errors. Using a static_cast, the compiler will check that you can really do what you claim you want to do (convert between two related types), while a C cast will let you do things and just generate invalid code if you are wrong -- good luck debugging that.

Additionally, that restriction come in handy if you use such casts in templated code. Though the types will only be known once the template is instanciated, using a static_cast guarantees that a bad C cast will not be generated, even indirectly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
The new cast-operators are more difficult to write, so you have two think twice before using one of them and they're easier to find (for humans or a search).


That's exactly what Stroustrup said when he explained to a reporter why he'd chosen that particular syntax.
http://public.research.att.com/~bs/bs_faq2.html

Share this post


Link to post
Share on other sites
Thanks for the answers!!!

Now I see it :))))

Btw great link, thx!
http://public.research.att.com/~bs/bs_faq2.html

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!