我是靠谱客的博主 迷你茉莉,这篇文章主要介绍c++中的类型转换--reinterpret_cast,现在分享给大家,希望可以做个参考。

reinterpret_cast作用为:
允许将任何指针转换为任何其他指针类型。 也允许将任何整数类型转换为任何指针类型以及反向转换。

语法还是老样子:
reinterpret_cast < type-id > ( expression )

滥用 reinterpret_cast 运算符可能很容易带来风险。 除非所需转换本身是低级别的,否则应使用其他强制转换运算符之一。

reinterpret_cast 运算符可用于 char* 到 int* 或 One_class* 到 Unrelated_class* 之类的转换,这本身并不安全。

reinterpret_cast 的结果不能安全地用于除强制转换回其原始类型以外的任何用途。 在最好的情况下,其他用途也是不可移植的。

reinterpret_cast 运算符不能丢掉 const、volatile 或 __unaligned 特性。

例如将 shared_ptr * b 转换成 int** a
int** a = reinterpret_cast<int**>(b);

复制代码
1
2
3
4
5
6
std::vector<std::shared_ptr<int> > param; param.push_back(std::make_shared<int>(5)); param.push_back(std::make_shared<int>(5)); std::shared_ptr<int>* b = param.data(); int** a = reinterpret_cast<int**>(b);

最后

以上就是迷你茉莉最近收集整理的关于c++中的类型转换--reinterpret_cast的全部内容,更多相关c++中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部