我想将void* 转换为char* reinterpret_cast 和static_cast,哪个适合? static_cast<char*> or reinterpret_cast<char*>
最佳答案
这主要是风格问题。 static_cast
可以做任意转换
这与隐式转换相反(并且不
删除 const
或 volatile
)。由于 char*
到 void*
是隐式的,
static_cast
似乎已指示;通常的规则是使用
在可能的情况下,static_cast
优先于 reinterpret_cast
。
鉴于这种使用特别危险,一些编码指南
可能更喜欢 reinterpret_cast
来表示这一事实。
关于c++ - 哪种转换在 C++ 中是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12260656/