What is casting in C++? What is a cast operator? What types of casting can be used in a C++ app? What is reinterpret_cast in C++? How can I use reinterpret_cast in C++?
C++ is a fast and powerful programming language suitable for all types of purposes.
In this post, we will explain how to use reinterpret_cast in C++. Before this, let’s recap what is meant by ‘casting’ in C++.
What is casting in C++?
Casting is a technique to convert one data type to another data type. The operator used for this purpose is known as the cast operator. It is a unary operator which forces one data type to be converted into another data type.
In C++, a cast operator is an Unary Operator which forces one data type to be converted into another data type.
In general, C++ supports four types of casting:
- Static Cast (static_cast)
- Dynamic Cast (dynamic_cast)
- Constant Cast (const_cast)
- Reinterpret Cast (reinterpret_cast)
What is reinterpret_cast?
ReInterpret Cast (reinterpret_cast) is a cast operator that converts a pointer of some data type into a pointer of another data type, even if the the data types before and after conversion are different. reninterpret_cast does not check if the pointer type and data pointed by the pointer is same or not. In another terms reinterpret_cast converts type to another type by reinterpreting the underlying bit pattern which is pointed by a pointer.
reinterpret_cast only guarantees that if you cast a pointer to a different type, and then
reinterpret_cast it back to the original type, you get the original value. So in the following:
Syntax for the reinterpret_cast:
new_data_type* pointer = reinterpret_cast < new_data_type* >( previous_data_type_pointer );
Is there an example of how to use reinterpret_cast?
This is a simple example of how to use reinterpret_cast.
int i = 100;
int* p = &i; // pointed adress of ptr_i is equal to the adress of i
long int* li = reinterpret_cast< long lint* >( p ); // reinterpret_cast from p to a new pointer form
Here is a full example
int i = 100; // write value 100 to the adress of i
std::cout << "Step 1: i = " << i << std::endl;
int *ptr_i = &i; // pointed adress of ptr_i is equal to the adress of i
std::cout << "Step 2: ptr_i = " << *ptr_i << std::endl;
*ptr_i = 101; // write value 101 to pointed adress;
std::cout << "Step 3: ptr_i = " << *ptr_i << std::endl;
long int* ptr_lli = reinterpret_cast<long int*>(ptr_i); // reinterpret_cast from ptr_i
std::cout << "Step 4: ptr_lli = " << *ptr_lli << std::endl;
*ptr_lli = 102; // write value 102 to pointed adress;
std::cout << "Step 5: ptr_lli = " << *ptr_lli << std::endl;
std::cout << " ptr_i = " << *ptr_i << std::endl;
int* ptr_i2 = reinterpret_cast<int*>( ptr_lli); // reinterpret_cast from ptr_lli
std::cout << "Spte 6: ptr_i2=" << *ptr_lli << std::endl;
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.