<分区>
考虑以下代码:
int** a;
const int** b;
b = a;
这段代码报错:
error C2440: '=' : cannot convert from 'int **' to 'const int **'
Conversion loses qualifiers
为什么我无法执行类型转换?
当操作简单的指针时,它工作正常。
int* a;
const int* b;
b = a;
标签 c++
<分区>
考虑以下代码:
int** a;
const int** b;
b = a;
这段代码报错:
error C2440: '=' : cannot convert from 'int **' to 'const int **'
Conversion loses qualifiers
为什么我无法执行类型转换?
当操作简单的指针时,它工作正常。
int* a;
const int* b;
b = a;
最佳答案
假设您能够执行此转换。考虑:
const int n = 42;
const int* cp = &n;
int* p;
int** a = &p;
const int** b;
b = a; // hypothetical, doesn't compile
*b = cp; // equivalent to p = cp;
*p = 84; // equivalent to n = 84: oops
因此,允许从 int**
到 const int**
的隐式转换将允许程序违反 const 正确性。
关于C++ 无法从 'int **' 转换为 'const int **',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209882/