https://en.cppreference.com/w/cpp/language/reinterpret_cast
An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of expression. (since C++11)
为什么我应该对将某些东西转换成它自己的类型感兴趣?这对我来说就像我想将 int 转换为 int。
请指正。
最佳答案
因为也许你不知道它是不是“它自己的类型”。也许你在一个模板函数中,你正在调用一些函数,你需要它的返回值是 reinterpret_cast
能够使用模板参数 T
.所以该函数返回类型为 T
的值是可以的.
举一个更有用的例子,假设给你一些类型为 T
的对象.然后你调用一个应该返回某种指针的函数。并且您想将此指针转换为 char*
并做一些低级别的困惑,也许将其传递给 memcpy
什么的。
好吧,如果函数返回 char*
会发生什么? ?也就是说,它返回了一个字符串。你需要reinterpret_cast<char*>
继续工作。
关于c++ - reinterpret_cast - 整数、枚举、指针或成员指针类型的表达式可以转换为它自己的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63635285/