C++中的reinterpret_cast用法介绍 |类型转换运算符

reinterpret_cast是C++中使用的一种类型转换运算符,下面我们来介绍C++ reinterpret_cast用法。

  • 它用于转换任何类型的另一个指针的一个指针, 而不管该类是否彼此相关。
  • 它不检查指针类型和指针所指向的数据是否相同。
reinterpret_cast用法语法:
data_type *var_name = reinterpret_cast < data_type *>(pointer_variable);

返回类型
  • 没有任何返回类型。它只是转换指针类型。
参数
  • 它仅采用一个参数, 即源指针变量(在上面的示例中为p)。
// 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; }

输出如下:
65 A 0x1609c20 A

使用reinterpret_cast的目的
  1. reinterpret_cast是一种非常特殊且危险的类型转换操作符。建议使用正确的数据类型使用它(指针数据类型应与原始数据类型相同)。
  2. 它可以将任何指针类型转换为任何其他数据类型。
  3. 当我们要使用位时使用它。
  4. 如果我们使用这种类型的演员表, 那么它将变成不可携带的产品。因此, 除非必要, 否则建议不要使用此概念。
  5. 它仅用于将任何指针转换为原始类型。
  6. 布尔值将转换为整数值, 即0表示false, 1表示true。
C++ reinterpret_cast用法示例代码1:
// 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; }

输出如下:
12 5 10 a 1 1

reinterpret_cast用法示例代码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; }

输出如下:
In class A

你还可以参考以下相关链接获取更多C++ reinterpret_cast用法的相关信息,例如下面stackoverflow对何时使用C++ reinterpret_cast有一些详细的讨论。
https://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast
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
【C++中的reinterpret_cast用法介绍 |类型转换运算符】以上就是C++中的reinterpret_cast用法介绍和代码示例,reinterpret_cast是一个类型转换运算符,它可以帮助我们实现数据类型转换,希望以上内容可以帮到你。

    推荐阅读