类型转换扯的淡

来源:互联网 发布:百度云会员淘宝关键字 编辑:程序博客网 时间:2024/04/29 00:59

今天在看一位作者写的多线程文章时中间有这么一句代码:

handle[i] = (HANDLE)_beginthreadex(arg...);

_beginthreadex()这个函数返回的是一个unitptr_t类型,而handle[i]是一个句柄数组,所以需要一步类型转换
当时我想到如果用C++11定义的类型转换可能会比较好一点吧,不用那种C风格字符串了。于是很自然的把代码改成了下面这样

handle[i] = staic_cast<HANDLE>(_beginthreadex(arg...));

不过编译器却报错了。报错为

:不能进行从整形到指针类型的转换

后来用reinterpret_cast搞定了
上了权威网站查了之后发现reinterpret_cast<new type>(expr)进行type*type 的类型转换,中间有这么一句话

A value of any integral or enumeration type can be converted to a pointer type

至于static_cast为什么不行?
static_cast
上面给的可以用static_cast进行转换的9种情况中,并没有type to *type

参考资料:
reinterpret_cast

0 0
原创粉丝点击