reinterpret_cast或C型类型转换
reinterpret_cast or C Style type casting
应该优先于reinterpret_cast而不是C样式的转换。请解释一下。
如果必须在 reinterpret_cast和 c 型铸造之间进行选择,应该首选哪一个
强制转换应该很少使用,而且要谨慎使用,如果你写:
char *x = const_cast<char *>(some_const_char_pointer_expression);
而不是用以下方式伪装它:
char *x = (char *)some_const_char_pointer_expression;
因此,请使用显式、受控、详细的表示法,因为它鼓励您避免强制转换,并在必须使用强制转换时使用正确、精确的强制转换。
C 样式的转换更通用。它从const_cast
、static_cast
、const_cast
static_cast
、dynamic_cast
中"选择",然后dynamic_cast
const_cast
,最后reinterpret_cast
,reinterpret_cast
const_cast
。所以,如你所见,c-cast和reinterpret_cast
之间有很大的区别,因为C-cast首先使用静态和动态cast。
如果你用C++写,你应该更喜欢上面的一个转换,而不是C样式的转换。它们更明确。但C型演员并不是非常非常糟糕的风格。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 逐位操作的隐式类型转换
- 模板中的类型转换
- 在 C++(和 C)中进行类型转换时明显不一致
- 字符类型转换不兼容
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- C++:用户定义的显式类型转换函数错误
- 将类指针类型转换为键时出错
- 通过引用传递参数时C++类型转换
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- C++显式类型转换(C 样式强制转换)的强制表示法和static_cast的多种解释
- C++无效的函数类型转换
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将Windows产品类型转换为名称?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 事件系统:使用类型转换或联合进行继承
- 如何在参数中定义隐式类型转换的构造函数?
- 类模板实例化中的类型转换