如何从char*转换为T*?

Mih*_*yan 6 c++ pointers casting

可能重复:
C++我们何时应该优先使用两个链式的static_cast而不是reinterpret_cast

哪个更好?

static_cast<T *>(static_cast<void *>(buffer));
Run Code Online (Sandbox Code Playgroud)

要么

reinterpret_cast<T *>(buffer);
Run Code Online (Sandbox Code Playgroud)

其中bufferchar *(存储器块包含类型的值T).

sha*_*oth 5

两个static_casts 的链条更好 - 它更少依赖于实现.