Sign in to follow this  

static_cast - why?

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

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