1

考虑以下代码:

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;
4

1 回答 1

3

假设您能够执行此演员表。考虑:

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

因此,允许隐式转换 from int**toconst int**将允许程序违反 const 正确性。

于 2013-10-06T14:29:42.380 回答