我有一个接收 float**
作为参数的函数,我尝试将其更改为采用 const float**
。
编译器(g++
)不喜欢,发出:
从‘float**’到‘const float**’的无效转换
这对我来说毫无意义,我知道(并已验证)我可以将 char*
传递给采用 const char*
的函数,所以为什么不使用 const float**
?
最佳答案
见 Why am I getting an error converting a Foo** → const Foo**?
Because converting
Foo**
→const Foo**
would be invalid and dangerous ... The reason the conversion fromFoo**
→const Foo**
is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast
该引用继续举例说明这种隐式转换如何允许我在不进行强制转换的情况下修改 const
对象。
关于c++ - 为什么将 ‘float**’ 转换为 ‘const float**’ 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2463473/