从 Foo** 到 void** 的无效转换 - 为什么允许隐式类型转换为 void* 但不允许为 void**?

2022-01-13 00:00:00 gcc type-conversion c++
struct Foo {};
...
void * p = (Foo*)0; // OK
void ** pp = (Foo**)0; // Invalid conversion

据我所知,指向任何非指针类型的指针都可以在 C++ 中隐式转换为 void*.那么为什么不允许将指针类型转换为 void**?

As far as I recall, a pointer to any non-pointer type can be implicitly cast to void* in C++. Why then is the same not allowed for casting a ponter to pointer type to void**?

推荐答案

指针可以隐式转换为 void * 因为 void * 是通用指针.但是,void ** 不是指向指针的通用指针.

A pointer can be implicitly cast to void * because void * is the generic pointer. However, void ** isn't the generic pointer to pointer.

C FAQ 4.9 解释了为什么 C 中没有指向指针类型的通用指针,我认为它也适用于 C++.

C FAQ 4.9 explains why there is no generic pointer to pointer type in C, I think it applies to C++ as well.

相关文章