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**?
基础教程推荐
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- c++ STL设置差异 2022-01-01
