考虑到以下代码(以及VirtualAlloc()
返回一个void*
):
BYTE* pbNext = reinterpret_cast<BYTE*>(
VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE));
为什么选择使用reinterpret_cast
而不是static_cast
?
我曾认为reinterpret_cast
可以用于将指针从整数类型(如DWORD_PTR
)转换并转换回来,但是将void*
转换为BYTE*
,使用static_cast
是否可行?
在这种特定情况下,它们有任何(微妙的)差异,还是它们都是有效的指针转换?
C++标准对于此案例有偏好吗?建议使用一种方法而不是另一种方法吗?
static_cast
是完全合适的。 - user529758static_cast
,但有些人更喜欢使用reinterpret_cast
,因为名称表明了你正在做什么(重新解释位模式)。 - Jesse Good