Invalid conversion from Foo** to void** - why is implicit type conversion allowed to void* but not to void**?(从 Foo** 到 void** 的无效转换 - 为什么允许隐式类型转换为 void* 但不允许为 void**?)
问题描述
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.
这篇关于从 Foo** 到 void** 的无效转换 - 为什么允许隐式类型转换为 void* 但不允许为 void**?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Foo** 到 void** 的无效转换 - 为什么允许隐式类型转换为 void* 但不允许为 void**?


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01