【发布时间】:2013-10-14 12:04:34
【问题描述】:
我正在查看一些提供 c api 的 c++ 包装器代码,我发现有很多 reinterpret_cast static_cast 就足够了,例如:
struct cpp_object{ void foo(){ /* do something */ } };
/* begin: c api */
typedef void c_object;
void foo(c_object *o)
{
// why this:
reinterpret_cast<cpp_object *>(o)->foo();
// instead of just:
static_cast<cpp_object *>(o)->foo();
}
/* end: c api */
通常我在极少数情况下使用reinterpret_cast,主要与将缓冲区内容强制位强制转换为已知布局和大小类型(已知位于缓冲区内容内)有关。
所以我问这种做法是否有意义或坚持static_cast 会更好。
【问题讨论】:
-
@JohnBartholomew 谢谢
标签: c++ casting coding-style conventions reinterpret-cast