我希望将一个unsigned long
(实际上是一个DWORD
)重新解释为一个signed long
。我尝试过:
DWORD x;
long y = reinterpret_cast<signed long>(x);
然而,VC++2010 智能提示告诉我“无效的类型转换”。为什么?我该如何解决?
我希望将一个unsigned long
(实际上是一个DWORD
)重新解释为一个signed long
。我尝试过:
DWORD x;
long y = reinterpret_cast<signed long>(x);
reinterpret_cast
来将无符号类型转换为有符号类型,static_cast
就可以实现。reinterpret_cast
的类型,将整数类型转换为另一种整数类型不在该列表中。 - Cat Plus Plusconst_cast<int>(<const int>)
),它允许您更改 const 修饰符。还有 static_cast (static_cast<int>)(<short>
),它允许您执行类型安全的强制转换(例如将基类转换为派生类)。还有动态转换 (dynamic_cast<derived_type>(base_type)
),它允许您在两种类型之间进行转换,前提是这两种类型之间存在合法的转换(如果没有转换,则返回 null)。最后,还有一些转换可以在不相关的类型之间进行转换 - reinterpret_cast reinterpret_cast<int>(<void *>)
和 C 样式转换 (int)<void *>
。