我正在尝试将指针转换为 int(或 unsigned int),无论我尝试什么,它都不想工作。
我已经尝试过static_cast<intptr_t>(obj)
,reinterpret_cast<intptr_t>(obj)
和 C 风格强制转换的各种组合, intptr_t
's, unsigned int
's, 我包括 stdint.h。从我读过的内容来看,我尝试过的许多事情之一应该可以工作。是什么赋予了?
我没有费心包含代码,因为它正是我所描述的,但既然你问了,我已经尝试了所有这些以及其他组合:
void myfunc(Foo* obj)
{
// ...
uintptr_t temp = reinterpret_cast<uintptr_t>(obj);
uintptr_t temp = static_cast<uintptr_t>(obj);
uintptr_t temp = (uintptr_t)obj;
intptr_t temp = reinterpret_cast<intptr_t>(obj);
intptr_t temp = static_cast<intptr_t>(obj);
intptr_t temp = (intptr_t)obj;
unsigned int temp = reinterpret_cast<unsigned int>(obj);
unsigned int temp = static_cast<unsigned int>(obj);
unsigned int temp = (unsigned int)obj;
// ...
}
他们都给出了完全相同的错误。