reinterpret cast in C++

A reinterpret_cast operator handles conversions between unrelated types.
>>-reinterpret_cast--<--Type-->--(--expression--)--------------><
The reinterpret_cast operator produces a value of a new type that has the same bit pattern as its argument. You cannot cast away a const or volatile qualification. You can explicitly perform the following conversions:
  • A pointer to any integral type large enough to hold it
  • A value of integral or enumeration type to a pointer
  • A pointer to a function to a pointer to a function of a different type
  • A pointer to an object to a pointer to an object of a different type
  • A pointer to a member to a pointer to a member of a different class or type, if the types of the members are both function types or object types
A null pointer value is converted to the null pointer value of the destination type.
Given an lvalue expression of type T and an object x, the following two conversions are synonymous:
  • reinterpret_cast<T&>(x)
  • *reinterpret_cast<T*>(&x)

// An example that uses reinterpret_cast.
#include <iostream>
using namespace std;
int main()
{
int i;
char *p = "This is a string";
i = reinterpret_cast<int> (p); // cast pointer to integer
cout << i;
return 0;
}

Here, reinterpret_cast converts the pointer p into an integer. This conversion represents a fundamental type change and is a good use of reinterpret_cast.

Source : Google

0 comments:

Post a Comment