“警告:使用旧式强制转换”在g ++中[重复]

问题描述 投票:37回答:2

可能重复:When should static_cast, dynamic_cast and reinterpret_cast be used?

使用此C ++代码,

char* a = (char*) b;

我收到警告warning: use of old-style cast

什么是新型演员?

c++ casting g++
2个回答
55
投票

[reinterpret_caststatic_castdynamic_castconst_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

3
投票

阅读本主题,以了解各种风格的C ++样式转换:

© www.soinside.com 2019 - 2024. All rights reserved.