reinterpret_cast in C++ | Geben Sie Casting-Operatoren ein
reinterpret_cast ist eine Art Casting-Operator, der in C++ verwendet wird.
- Es wird verwendet, um einen Zeiger eines anderen Zeigers eines beliebigen Typs zu konvertieren, unabhängig davon, ob die Klasse miteinander verwandt ist oder nicht.
- Es prüft nicht, ob der Zeigertyp und die Daten, auf die der Zeiger zeigt, gleich sind oder nicht.
Syntax :
data_type *var_name = reinterpret_cast <data_type *>(pointer_variable);
Rückgabetyp
- Es hat keinen Rückgabetyp. Es konvertiert einfach den Zeigertyp.
Parameter
- Es braucht nur einen Parameter, dh die Quellenzeigervariable (p im obigen Beispiel).
// 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; }
Ausgabe:
65 EIN 0x1609c20 EIN
Zweck der Verwendung von reinterpret_cast
- reinterpret_cast ist eine sehr spezielle und gefährliche Art von Casting-Operator. Und es wird empfohlen, es mit dem richtigen Datentyp zu verwenden, dh (der Datentyp des Zeigers sollte mit dem ursprünglichen Datentyp identisch sein).
- Es kann jeden Zeiger auf jeden anderen Datentyp umwandeln.
- Es wird verwendet, wenn wir mit Bits arbeiten wollen.
- Wenn wir diese Art von Gips verwenden, wird es zu einem nicht tragbaren Produkt. Es wird daher empfohlen, dieses Konzept nur dann zu verwenden, wenn es erforderlich ist.
- Es wird nur verwendet, um einen Zeiger auf seinen ursprünglichen Typ umzuwandeln.
- Der boolesche Wert wird in einen ganzzahligen Wert umgewandelt, dh 0 für falsch und 1 für wahr.
// 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; }
Ausgabe:
12 5 10 ein 1 1
Programm 2
// 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; }
Ausgabe:
In Klasse A
Zugehöriger 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