Reinterpret_cast – cpp

The casting of variables is one way to convert one type of variable to another, for example if you wanted to have Pi being a double number e.g. 3.14 and then wanted to have the integer value instead (just the whole number) then that is a cast of the variable.

The classic was of casting in C language is like

double pi = 3.14;
int piInt = (int)pi;

and the (int) is the casting of one type to another.

But it is better to use the c++ casting techniques, static_cast, const_cast, dynamic_cast and reinterpret_cast. I am going to do a reinterpret_cast, and this is a way of converting a pointer to a variable from one type to another that is not of a similar type and it will try and fit the cast into the return type.

The syntax for reinterpret_cast is

returnvalue = reinterpret_cast<return type*>(casting value);

where the return value is what is casted and the return type is what you want to be casted to, and the casting value is what you want to be casting from.

One good example is how to use the reinterpret_cast is to convert one type into a void pointer (void *) and then back again, it is not really much use in this case and also reinterpret_cast does not do any type checking etc, it will just try and fit the casting value into the return type and if it fits, great, if it does not, great, it really does not care, so that is why it is better to use the static_cast (shall do a tutorial on that next).

#include <iostream>
 
int main()
{
  int *aInt = new int(10);
  cout << "A value = " << *aInt << endl;
 
  void *bVoid = reinterpret_cast<void*>(aInt);
  int *aBack = reinterpret_cast<int*>(bVoid);
 
  cout << "COME BACK TO ME !! A value again = " << *aBack << endl;
}

and the output would be

A value = 10
COME BACK TO ME !! A value again = 10

Since you are playing with pointers to memory locations then if you alter the value of A in that example the returning back cast would also reflect the new value as well.

Here is a example of just that, altering the value within the casted variable and also reinterpret_cast does not matter if you are using standard variable types like int,float etc.. it can also work on classes as well.

#include <iostream>
 
using namespace std;
 
class classA
{
  public:
    int valueX;
    int valueY;
 
    classA() { valueX = 0; valueY = 0;}
 
};
 
int main()
{
  classA *a = new classA();
  a->valueX = 10;
  a->valueY = 30;
  cout << "Value of X = " << a->valueX << " Value of Y = "  << a->valueY << endl;
 
  void *aClassVoid = reinterpret_cast<void*>(a);
 
  a = reinterpret_cast<classA*>(aClassVoid);
 
  cout << "COME BACK To me !! Value of X = " << a->valueX << " Value of Y = "  << a->valueY << endl;
 
  cout << "Value of X = " << a->valueX << " Value of Y = "  << a->valueY << endl;
  aClassVoid = reinterpret_cast<void*>(a);
  // but if you alter the values within a variable once you have done the first cast.
  cout << "After the first cast .. Value of X = " << a->valueX << " Value of Y = "  << a->valueY << endl;
  a->valueX = 0;
  cout << "After settings the value to 0 .. Value of X = " << a->valueX << " Value of Y = "  << a->valueY << endl;
  //and try again with the casting with the aClassVoid
  classA *AP = reinterpret_cast<classA*>(aClassVoid);
 
  cout << "COME BACK To me !! Value of X = " << AP->valueX << " Value of Y = "  << AP->valueY << endl;
 
  cout << "The last reinterpret_cast leaves the value as 0 for valueX because it is still only pointing to the same place as 'a'" << endl;
 
   return 0;
}

and the output is

Value of X = 10 Value of Y = 30
COME BACK To me !! Value of X = 10 Value of Y = 30
Value of X = 10 Value of Y = 30
After the first cast .. Value of X = 10 Value of Y = 30
After settings the value to 0 .. Value of X = 0 Value of Y = 30
COME BACK To me !! Value of X = 0 Value of Y = 30
The last reinterpret_cast leaves the value as 0 for valueX because it is still only pointing to the same place as 'a'

Hope that helps in how to use reinterpret_cast, but as stated before, best to use static_cast because that would give some information if the casting was successful. Also reinterpret_cast can also be used on function pointers to, which is kinder cool. The main reason for reinterpret_cast is that it uses minimal type checking, and if the target as similar bit patterns to the original then we are good to go as such in the reinterpret_cast way of things.

Leave a Reply

Your email address will not be published. Required fields are marked *