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++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01