为什么我必须重新解释指针指针?

Jon*_*Mee 3 c++ void-pointers double-pointer static-cast reinterpret-cast

所以这段static_cast代码完全合法:

int n = 13;
void* pn = static_cast<void*>(&n);
void** ppn = &pn;
Run Code Online (Sandbox Code Playgroud)

然而,这必须编成一个reinterpret_cast编译:

int n = 13;
int* foo = &n;
void** bar = static_cast<void**>(&foo);
Run Code Online (Sandbox Code Playgroud)

如果我不改变它我得到错误:

错误C2440 :: static_cast无法转换int **void ** 注释:指向的类型不相关; 转换需要reinterpret_cast,C风格的演员或功能风格演员

所以我认为问题是"类型无关".然而,我还是不明白,如果它从去时是确定int*void*他们怎么能不相关的int**void**

eer*_*ika 7

int与...无关void.同样适用于int**,void**因此无法使用它们进行转换static_cast.

void*但是,很特别.任何数据指针类型(包括int*)都可以static_cast进入void*和返回,尽管没有类型相关void(更进一步,转换为void*不需要强制转换,因为它是隐含的).int*没有这个属性,也没有void**,也没有任何其他指针void*.


已经授予void*额外限制的额外自由.void*不能被指向,也不能用于指针算术.这些限制是唯一可能的,因为永远不会有类型的对象void.或者从相反的角度来看,void由于这些限制,对象不可能存在.

void**不能给予这些自由,因为它不能给予同样的限制.它不能被赋予这些限制,因为void*对象确实存在并且它们需要存在.如果我们不能间接或迭代void**,那么我们就不能使用void*例如数组.