问题描述
为什么我会进入这个代码:
Why do I get in this code:
这个错误:
我认为可以从非常量转换为常量.
I thought it would be possible to convert from non-const to const.
推荐答案
那是因为你试图从 int** 转换为 const int**
int **
是:指向整数指针的指针".const int **
是:一个指向常量整数指针的指针".int **
is: "a pointer to a pointer to an integer".const int **
is: "a pointer to a pointer to a constant integer".
const
的使用是一个契约,你不能通过两个引用的间接来满足这个契约.
The use of const
is a contract and you cannot meet this contract by going through the indirection of two references.
来自标准:
因此可以修改const char
总是,只需使用上面的过程.
so it would be possible to modify const char
always, just use procedure above.
还有:
从下面的链接中引用:
打个比方,如果你用合法的伪装隐藏一个罪犯,他然后可以利用对这种伪装的信任.这很糟糕.
By way of analogy, if you hide a criminal under a lawful disguise, he can then exploit the trust given to that disguise. That's bad.
http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html
与此相关的也是无效转换Derived** → Base**
.如果转换 Derived** → Base**
是合法的,则可以取消引用 Base**
(产生 Base*
),并且可以使 Base* 指向不同派生类的对象,这可能会导致严重的问题.看看为什么:
related to this is also invalid conversion Derived** → Base**
. If it were legal to convert Derived** → Base**
, the Base**
could be dereferenced (yielding a Base*
), and the Base* could be made to point to an object of a different derived class, which could cause serious problems. See why:
http://www.parashift.com/c++-faq-lite/derivedptrptr-to-baseptrptr.html
考虑:
这篇关于从 int** 到 const int** 的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!