有了这段C++代码,
char* a = (char*) b;
我收到了警告信息warning: use of old-style cast
。
新式转换会是什么样的?
有了这段C++代码,
char* a = (char*) b;
我收到了警告信息warning: use of old-style cast
。
新式转换会是什么样的?
reinterpret_cast
、static_cast
、dynamic_cast
和 const_cast
是 c++ 中的类型转换操作符。
const_cast
用于去除变量的 const/volatile 限定符。dynamic_cast
用于在多态类型之间进行强制类型转换时运行时执行有效性检查。static_cast
用于在继承层次结构中执行向上/向下类型转换,并执行没有运行时检查的显式转换(例如将 float 转换为 int)。reinterpret_cast
用于在不相关的类型之间进行转换。简短的语法示例:
char* a = (char*) b;
//would be
char* a = static_cast<char*>(b);
//to remove the warning
阅读本主题,了解有关C++风格转换的各种变体:
b
是什么? - GManNickG