reinterpret_cast in C++ | Type Casting operators Last Updated : 30 May, 2022 Comments Improve Suggest changes Like Article Like Report reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different.It does not check if the pointer type and data pointed by the pointer is same or not. Syntax : data_type *var_name = reinterpret_cast <data_type *>(pointer_variable); Return Type It doesn't have any return type. It simply converts the pointer type. Parameters It takes only one parameter i.e., the source pointer variable (p in above example). CPP // CPP program to demonstrate working of // reinterpret_cast #include <iostream> using namespace std; int main() { int* p = new int(65); char* ch = reinterpret_cast<char*>(p); cout << *p << endl; cout << *ch << endl; cout << p << endl; cout << ch << endl; return 0; } Output: 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. CPP // CPP code to illustrate using structure #include <bits/stdc++.h> using namespace std; // creating structure mystruct struct mystruct { int x; int y; char c; bool b; }; int main() { mystruct s; // Assigning values s.x = 5; s.y = 10; s.c = 'a'; s.b = true; // data type must be same during casting // as that of original // converting the pointer of 's' to, // pointer of int type in 'p'. int* p = reinterpret_cast<int*>(&s); cout << sizeof(s) << endl; // printing the value currently pointed by *p cout << *p << endl; // incrementing the pointer by 1 p++; // printing the next integer value cout << *p << endl; p++; // we are casting back char * pointed // by p using char *ch. char* ch = reinterpret_cast<char*>(p); // printing the character value // pointed by (*ch) cout << *ch << endl; ch++; /* since, (*ch) now points to boolean value, so it is required to access the value using same type conversion.so, we have used data type of *n to be bool. */ bool* n = reinterpret_cast<bool*>(ch); cout << *n << endl; // we can also use this line of code to // print the value pointed by (*ch). cout << *(reinterpret_cast<bool*>(ch)); return 0; } Output: 12 5 10 a 1 1 Program 2 CPP // CPP code to illustrate the pointer reinterpret #include <iostream> using namespace std; class A { public: void fun_a() { cout << " In class A\n"; } }; class B { public: void fun_b() { cout << " In class B\n"; } }; int main() { // creating object of class B B* x = new B(); // converting the pointer to object // referenced of class B to class A A* new_a = reinterpret_cast<A*>(x); // accessing the function of class A new_a->fun_a(); return 0; } Output: In class A Related link : https://www.geeksforgeeks.org/casting-operators-in-c-set-1-const_cast/ https://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast" rel="noopener" target="_blank http://forums.codeguru.com/showthread.php?482227-reinterpret_cast-lt-gt-and-where-can-it-be-used https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/keyword_reinterpret_cast.htm https://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast Comment More infoAdvertise with us Next Article reinterpret_cast in C++ | Type Casting operators R Rohit_ranjan Follow Improve Article Tags : C++ cpp-pointer cpp-advanced Practice Tags : CPP Similar Reads const_cast in C++ | Type Casting operators C++ supports following 4 types of casting operators: 1. const_cast 2. static_cast 3. dynamic_cast 4. reinterpret_cast 1. const_cast const_cast is used to cast away the constness of variables. Following are some interesting facts about const_cast. 1) const_cast can be used to change non-const class m 4 min read Casting Operators in C++ The casting operators is the modern C++ solution for converting one type of data safely to another type. This process is called typecasting where the type of the data is changed to another type either implicitly (by the compiler) or explicitly (by the programmer).Let's take a look at an example:C++# 5 min read Types of Operator Overloading in C++ C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change some specific operators' functions to do different tasks.Syntax: Return_Type classname :: op 4 min read RTTI (Run-Time Type Information) in C++ In C++, RTTI (Run-time type information) is a mechanism that exposes information about an object's data type at runtime and is available only for the classes which have at least one virtual function. It allows the type of an object to be determined during program execution. Runtime Casts The runtime 3 min read Typecast Operator Overloading in C++ In C++, the typecast operator can be overloaded to customize the behavior of casting operators to define how user-defined data types can be converted into other types. This enables developers to define how instances of a class are converted to other types, providing more control over implicit type c 6 min read Like