【发布时间】:2011-07-12 02:07:20
【问题描述】:
可能重复:
When should static_cast, dynamic_cast and reinterpret_cast be used?
有了这个 C++ 代码,
char* a = (char*) b;
我收到警告warning: use of old-style cast。
新风格的演员阵容是什么?
【问题讨论】:
-
好吧,
b是什么?
可能重复:
When should static_cast, dynamic_cast and reinterpret_cast be used?
有了这个 C++ 代码,
char* a = (char*) b;
我收到警告warning: use of old-style cast。
新风格的演员阵容是什么?
【问题讨论】:
b 是什么?
reinterpret_cast、static_cast、dynamic_cast 和 const_cast 是 c++ cast 的替代品。
const_cast 从 const 变量中删除 const/volatile。dynamic_cast 在多态类型之间进行强制转换时执行运行时有效性检查static_cast 在继承层次结构中执行例如向上/向下转换,但没有运行时检查,或者显式执行可能是隐式的转换(例如从浮点数到整数)reinterpret_cast 在不相关的类型之间进行转换。简要语法示例:
char* a = (char*) b;
//would be
char* a = static_cast<char*>(b);
//to remove the warning
【讨论】:
阅读本主题以了解各种风格的 C++ 风格转换:
When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
【讨论】: