reinterpret_cast with Example

reinterpret_cast<type> (expr): The reinterpret_cast operator changes a pointer to any other type of pointer. It also allows casting from pointer to an integer type and vices versa.

We can convert any pointer type to any other pointer type using reinterpret_cast. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.

It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it is guaranteed to be able to be cast back to a valid pointer.

See the below example:

This code compiles, although it does not make much sense, since now b points to an object of a totally unrelated and likely incompatible class. Dereferencing b is unsafe.

Recommended Tutorials: