Search code examples
c++castingreinterpret-cast

reinterpret_cast


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.


Solution

  • 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.