C++

C++ Reinterpret_Cast

A pointer is always returned by the reinterpret_cast. Reinterpret_cast may execute the incorrect thing and tends to make the appropriate modification to the pointer if B has much more than an abstract class and A is not the first abstract class.

Return Type of the Reinterpret_Cast

There is no return type for it. The pointer type is only converted.

Parameters of the Reinterpret_Cast

The reference pointer variable is the only argument it accepts.

Use of Reinterpret_Cast

    • The casting operator reinterpret_cast is a unique and problematic one. It is recommended to utilize it with the appropriate data type.
    • Any pointer becomes a typecast to some other data type.
    • It is utilized while working with bits.
    • If we utilize the reinterpret_cast, the product lacks its portability. It is recommended not to employ this notion unless it is strictly essential.
    • It is exclusively employed for typecasting any pointer back to its real type.
    • The Boolean value is transformed into a binary number.

Let’s talk more about the C++ interpret_cast.

Example 1:

In this example, we’ll show how the reinterpret_cast functions at its most fundamental level.

#include <iostream>
using namespace std;

int main()
{
    int* a = new int(90);
    char* chr = reinterpret_cast<char*>(a);
    cout << *a << endl;
    cout << *chr << endl;
    cout << a << endl;
    cout << chr << endl;
    return 0;
}

 

We start the code by integrating the header file <iostream>. The standard namespace is used in the next step. We define the main() function. Within the function, we construct a pointer. Along with this, we initialize a variable and provide a value of this pointer.

Next, we declare a pointer of the character data type. We use the reinterpret_cast command. Here, we pass the character pointer. Furthermore, we utilize the cout statement first to print the variable pointer as well as the character pointer. Then, we employ the cout statement to display the values of both pointers. In the end, we apply the return 0 command.

Example 2:

This code demonstrates how to utilize the structure as an illustration.

#include <iostream>
using namespace std;

struct mystruct {
    int i;
    int j;
    char m;
    bool n;
};

int main()
{
    mystruct e;

    e.i = 25;
    e.j = 40;
    e.m = 'u';
    e.n = true;

    int* ptr = reinterpret_cast<int*>(&e);

    cout << sizeof (e) << endl;
    cout << *ptr << endl;

    ptr++;
    cout << *ptr << endl;

    ptr++;
    char* chr = reinterpret_cast<char*>(ptr);
    cout << *chr << endl;

    chr++;

    bool* b = reinterpret_cast<bool*>(chr);
    cout << *b << endl;
    cout << *(reinterpret_cast<bool*>(chr));

    return 0;
}

 

The <iostream> library and standard namespace are used at the commencement of the code. We create a structure termed mystruct. Within this structure, we initialize two variables named i and j. Then, we create two other variables from which the first variable has a character data type and the second variable has a Boolean data type. In the later step, we use the main() function. Inside this function, we call a variable “e” of the structure “mystruct”.

Then, we assign some random values to the variable associated with the structure. The first two variables contain the integer values. The third variable contains the character. The last variable contains the Boolean value. During casting, the data type of the variable is the same as that of the real variable. Now, we convert the pointer of “e” to the pointer of the variable having the integer data type. We construct the pointer and set its value equal to the reinterpret_cast. Then, we utilize the cout statement. We increment the value of the pointer “ptr” by 1. Now, we print the value of the next pointer, so we employ the cout statement again.

Additionally, this pointer’s value is raised by 1. We apply the casting to the pointer of the character “chr” by the use of reinterpret_cast. Now, just show the value of the character pointer with the help of the cout statement. As *chr already corresponds to a Boolean value, a similar data type transformation is employed to obtain the value. Thus, we use the data type *b, which is a Bool. We utilize the reinterpret_cast for the Boolean pointer. We print the value of that Boolean variable by applying the cout statement. The reinterpret_cast is employed to display the value pointed by the *chr. To terminate the code, use the return 0 statement.

Example 3:

The pointer reinterpret is demonstrated in this instance.

#include <iostream>
using namespace std;

class U {
public:
    void fun_x()
    {
        cout << " Present in class U\n";
    }
};

class V {
public:
    void fun_b()
    {
        cout << " Present in class V\n";
    }
};

int main()
{
    V* i = new V();

    V* new_x = reinterpret_cast<U*>(i);

    new_x->fun_x();
    return 0;
}

 

First of all, we introduce the module <iostream> and the standard namespace. Then, we construct a class named U. We define a function of this class publicly. We utilize the cout command within this function. Then, we make a new class called V. After calling the function, we make it public. Here, the cout statement is used. We start the coding inside the main() method. First, we construct the object “i” of class V. We transform the pointer to the object and give the reference of the class V to class U, so we utilize the reinterpret_cast. Next, we retrieve the function “fun_x” of class U. Then, apply the return 0 command to end the program.

Conclusion

We discussed the C++ reinterpret_cast in this article. There is a casting operator named reinterpret_cast in C++. No matter whether the classes are connected or not, it’s utilized to transform one pointer of one data type into some other pointer. We evaluated three examples and in one of them, we can see how to use the reinterpret_cast operates in its most basic form. In the second example, we make use of a structure and then use the reinterpret_cast to change its pointer to another pointer. The last illustration shows a pointer reinterpret.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.