reinterpret_cast is a type of casting operator used in C++.
- It is used to convert one pointer of another pointer of any type, no matter either the class is related to each other or not.
- It does not check if the pointer type and data pointed by the pointer is same or not.
data_type *var_name = reinterpret_cast <data_type *>(pointer_variable);
- It doesn’t have any return type. It simply converts the pointer type.
- It takes only one parameter i.e., the source pointer variable (p in above example).
65 A 0x1609c20 A
Purpose for using reinterpret_cast
- reinterpret_cast is a very special and dangerous type of casting operator. And is suggested to use it using proper data type i.e., (pointer data type should be same as original data type).
- It can typecast any pointer to any other data type.
- It is used when we want to work with bits.
- If we use this type of cast then it becomes a non-portable product. So, it is suggested not to use this concept unless required.
- It is only used to typecast any pointer to its original type.
- Boolean value will be converted into integer value i.e., 0 for false and 1 for true.
12 5 10 a 1 1
In class A
Related link :
https://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast” rel=”noopener” target=”_blank
- static_cast in C++ | Type Casting operators
- const_cast in C++ | Type Casting operators
- Type of 'this' pointer in C++
- Type Conversion in C++
- Is there any need of "long" data type in C and C++?
- Bool data type in C++
- Array Type Manipulation in C++
- Can a C++ class have an object of self type?
- Catch block and type conversion in C++
- C++ map having key as a user define data type
- Function overloading and return type
- Data type of character constants in C and C++
- Type difference of character literals in C and C++
- RTTI (Run-time type Information) in C++
- C++ set for user define data type
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.