views:

140

answers:

4
+1  Q: 

C++ type casting

Possible Duplicate:
When should static_cast, dynamic_cast and reinterpret_cast be used?

Until a few days ago, I've always used C style type casting in C++ because it seemed to work good. I recently found out that using C in C++ is very bad..

I've never really used C++ casting before, so I'm wondering if someone could tell me (in their own words preferably) what the difference between static_cast, reinterpret_cast and const_cast are?

const_cast I know removes a "const" from something, but I'm not sure what the difference between them all is, and what one I need to use in different situations.

A: 

static_cast - is just the c cast, eg. (int)1.000. it doesn't cost anything and can't fail. But it's only value is syntactic sugar (it's useful for searching in the editor)

reinterpret_cast - is the c++ equivalent of (void*). It could blow up in your face. Use this to tell the compiler to just do it, and the other programmers to be very careful.

dynamic_cast is a safer version that returns null if the conversion can't be done. This has a small runtime cost.

See also http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used

Martin Beckett
This is incorrect. A C-cast does much more than `static_cast`, and `reinterpret_cast` never returns null.
GMan
sorry but that is not correct. reinterpret_cast is the normal c-cast. static_cast is a cast checked at compile time, i think you are thinking of dynamic_cast
Anders K.
It's not correct to say that `reinterpret_cast` is the same as a C cast. Not every C cast can be built using C++ casts, and those that can may require either `static_cast` or `reinterpret_cast` and optionally `const_cast`.
Ben Voigt
A: 

static_cast<TYPE>(e-of-TYPE2) is a safe cast. It means that there is a convert from TYPE2 to TYPE1.

reinterpret_cast is close to a C cast in that it allows pretty much any conversion (with some limitations). The compiler expects you to know the type conversion is correct.

One thing that neither static_cast nor reinterpret_cast are allowed to do is remove a const. I.E. if you have a const char * and need to cast it to a char *, neither static_cast nor reinterpret_cast will allow that. Instead, const_cast is your friend; const_cast is used for removing a const modifier from a type.

R Samuel Klatchko
`const_cast` is not your friend -- don't get too chummy with it, it's your tool of last resort. 99% of situations which appear to need `const_cast` actually need refactoring.
Ben Voigt
the limitation of reinterpret_cast will be : both source and destination type must have same size.
YeenFei
A: 

To say "C casting is bad" is an extremity that by itself is about as bad as using C-style casts all the time.

The areas where "new" C++ style casts should be used are: hierarchical casts (upcasts, downcasts, crosscasts), const-correctness casts and reinterpretation casts. For arithmetical casts C-style casts work perfectly fine and pose no danger, which is why they can safely be used in C++ code. In fact, I would actually recommend using specifically C-style casts as arithmetical casts - just to make arithmetical casts to look different from other cast types.

AndreyT
C-style casts (1) can't be found with a simple search and (2) do bad things with no warning. So minimizing C-style casts is a good thing. Casts of numeric types can be done with constructor-style casts which admittedly still suffer from (1) but not from (2).
Ben Voigt
@Ben Voigt: Firstly, the issue of searching for casts does not appear as something practically useful to me, if the casts were used within a system of well-thought-through convention (for example, as the one I describe above). And, no, they don't do bad things with no warning when used this way. Secondly, what would be the point of using functional-style cars if they are in no way better than C-style casts? Also, functional-style cannot be used with multi-token type names, like `unsigned int` for example.
AndreyT
+1  A: 
  1. static_cast is the standard c++ way to do a cast at compile time when programmer knows the type of an object and/or wants to let the compiler know.
  2. dynamic_cast is like '(T)obj' where the cast is checked at runtime.
  3. reinterpret_cast is used to cast between different objects without a runtime check.
  4. const_cast explicitly converts to a type that is identical by removing the const and volatile qualifiers.
Prabhu Jayaraman