6

In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book, and in chapter (8), it mentions the following about reinterpret_cast

....converts from one pointer type (int) to another (char*). Because the cast changes the way the data pointed to is interpreted, it is called reinterpret_cast, as opposed to static_cast.*

Can you describe this paragraph here? Especially the reason for the way the operation is named?

Thanks.

Sisir
  • 4,584
  • 4
  • 26
  • 37
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 3
    That's a great name for a book! – Joe Jan 20 '11 at 14:24
  • There was another question today that came out of this book, and it ended up doing anything but make the OP feel smart. – Lightness Races in Orbit Jan 20 '11 at 14:32
  • Ah, hah, it was you. http://stackoverflow.com/questions/4746980/c-quitting-a-program Shame you opted not to take my advice about getting a real one. – Lightness Races in Orbit Jan 20 '11 at 14:32
  • 1
    @Joe: It's a name I have never heard of. Maybe I'm to conservative here, but if it's not recommended on [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), I'm a skeptic. C++ is a very complex beast, complex enough that there's hundreds of books out there which get the basic facts wrong. I wouldn't suggest any C++ book that isn't recommended by the (experienced part of the) community. – sbi Jan 20 '11 at 14:53
  • Try reading this article: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=134 – Tim Bish Jan 20 '11 at 14:28

2 Answers2

9

Basically, reinterpret_cast reinterprets the bit pattern at a specific location as a different type.

See for example here: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05keyword_reinterpret_cast.htm

The reinterpret_cast operator produces a value of a new type that has the same bit pattern as its argument.

A static cast converts the argument instead of just reinterpreting it. You can try this out by static_casting an int to float and reinterpret_casting an int to float. The result will be totally different.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
  • 4
    It doesn't necessarily have the same bit pattern. There are very few guarantees on `reinterpret_cast` in the Standard. 5.2.10/3 says "The mapping performed by `reinterpret_cast` is implementation-defined. [Note: it might, or might not, produce a representation different from the original value.]" Also, a `reinterpret_cast` cannot cast an `int` to a `float` (it can cast `int *` to `float *`). Any implementation that does that cast is in violation of the Standard (5.2.10/1, last sentence), although it's probably a common enough extension. – David Thornley Jan 20 '11 at 14:43
4

There's nothing fancy here. it's really just intended to reinterpret something.

From standard 5.3.10, reinterpret_cast is aimed to cater the following cases:

  • A pointer can be explicitly converted to any integral type large enough to hold it.
  • A value of integral type or enumeration type can be explicitly converted to a pointer.
  • A pointer to a function can be explicitly converted to a pointer to a function of a different type.
  • A pointer to an object can be explicitly converted to a pointer to a different object type.
  • Converting a pointer to a function into a pointer to an object type or vice versa is conditionally-supported.
  • The null pointer value (4.10) is converted to the null pointer value of the destination type.
  • A prvalue of type “pointer to member of X of type T1” can be explicitly converted to a prvalue of a different type “pointer to member of Y of type T2” if T1 and T2 are both function types or both object types.
  • An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. That is, a reference cast reinterpret_cast < T& >(x) has the same effect as the conversion *reinterpret_cast< T* >(&x) with the built-in & and * operators (and similarly for reinterpret_cast< T&& >(x)).
user534498
  • 3,926
  • 5
  • 27
  • 52