Should I use static_cast or reinterpret_cast when casting a void* to whatever

后端 未结 4 893
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 16:08

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?

4条回答
  •  星月不相逢
    2020-11-22 17:04

    I suggest using the weakest possible cast always.

    reinterpret_cast may be used to cast a pointer to a float. The more structure-breaking the cast is, the more attention using it requires.

    In case of char*, I'd use c-style cast, until we have some reinterpret_pointer_cast, because it's weaker and nothing else is sufficient.

提交回复
热议问题