6

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.

Community
  • 1
  • 1
Brad
  • 223
  • 1
  • 4
  • 10
  • You should probably get a good book and learn C++ properly. :) http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list And there's a good summary here: http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast/1255015#1255015 – GManNickG Jul 19 '10 at 04:26
  • 1
    And here http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used – Martin Beckett Jul 19 '10 at 04:31
  • 1
    It's not "very bad", it's dangerous, requires some care, and has maintenance pitfalls. Same as use of raw pointers, actually. In C you have no better option but C++ gives you tools to express your meaning and let the compiler produce better warnings and errors. – Ben Voigt Jul 19 '10 at 04:56

4 Answers4

3

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 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 Jul 19 '10 at 04:52
  • @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. – AnT stands with Russia Jul 19 '10 at 07:53
2
  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.
josh
  • 13,793
  • 12
  • 49
  • 58
1

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
  • 74,869
  • 16
  • 134
  • 187
  • 1
    `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 Jul 19 '10 at 04:49
  • the limitation of reinterpret_cast will be : both source and destination type must have same size. – YeenFei Jul 19 '10 at 05:17
-2

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 When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

Community
  • 1
  • 1
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • This is incorrect. A C-cast does much more than `static_cast`, and `reinterpret_cast` never returns null. – GManNickG Jul 19 '10 at 04:28
  • 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 – AndersK Jul 19 '10 at 04:29
  • 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 Jul 19 '10 at 04:48