c++ - 为什么将 ‘float**’ 转换为 ‘const float**’ 时出现错误?

标签 c++ const-correctness

我有一个接收 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 from Foo**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/

相关文章:

c++ - 如何在 C++ 中将 SFML 图像转换为 OpenCV mat?

c++ - 通过宏定义编译标志(C++11 和优化)

c++ - Bison 不解析多行?

c++ - 返回值的初始化应该忽略自动对象的常量性

c++ - 如何使可变 lambda 捕获列表的某些成员成为 const?

c++ - 普通类型的类型特征

c++ - 返回对类成员容器的元素的引用

c++ - 为什么很少有人输入 const 正确的代码? const 正确的代码会编译得更好/更快吗?

c++ - 我应该声明任何可以是 const 的方法吗?

C++ 常量赋值