为什么我在转换“float**”时出错到“const float**”?
我有一个接收 float**
作为参数的函数,我尝试将其更改为采用 const float**
。
编译器(g++
)不喜欢它并发出:
invalid conversion from 'float**' to 'const float**'
这对我来说毫无意义,我知道(并已验证)我可以将 char*
传递给采用 const char*
的函数,那么为什么不使用 const float**
呢?
I have a function that receives float**
as an argument, and I tried to change it to take const float**
.
The compiler (g++
) didn't like it and issued :
invalid conversion from ‘float**’ to ‘const float**’
this makes no sense to me, I know (and verified) that I can pass char*
to a function that takes const char*
, so why not with const float**
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅 为什么在转换 Foo** → const Foo** 时出现错误?< /a>
该参考文献继续给出了一个示例,说明这种隐式转换如何允许我修改 <没有强制转换的 code>const 对象。
See Why am I getting an error converting a Foo** → const Foo**?
The reference goes on to give an example of how such an implicit conversion could allow me one to modify a
const
object without a cast.这是一个非常棘手的限制。它与语言的别名规则有关。看看标准是怎么说的,因为我以前遇到过一次:
This is a very tricky restriction. It is related to the aliasing rules of the language. Take a look at what the standards say, because I have faced this once before:
其他答案详细说明了为什么这是 C++ 中的错误。
让我来解答你问题背后的问题。您想在函数的接口中声明您的函数不会修改数组中包含的浮点值。良好的意图,并允许使用 const float ** 数组调用您的函数。你的问题背后的问题是,如何在不解决丑陋的演员阵容的情况下实现这一目标。
实现您想要的正确方法是将函数参数的类型更改为 const float * const * 。
星号之间的附加 const 向编译器保证您的方法不会尝试在数组中存储指向 const float 的指针,因为此类型声明指针值也是 const。
您现在可以使用
float **
(这是您问题中的示例)、const float **
和const float * const *
调用此函数代码> 参数。Other anwers have detailled why this is an error in C++.
Let me address the question behind your question. You wanted to state, in the interface of your function, that your function will not modify float values contained in the array. Nice intention, and enables that your function is called with
const float **
arrays. The question behind your question would be, how to achieve this without resolving to ugly casts.The correct way to achieve what you wanted is to change the type of your function parameter to
const float * const *
.The additional
const
between the stars assures the compiler that your method will not try to store pointers to const float in the array, since this type declares that the pointer values are also const.You can now call this function with
float **
(which was the example in your question),const float **
, andconst float * const *
arguments.如果将参数转换为 const float**,则可以将 const float* 存储在参数指向的内存位置。但是调用函数认为这个内存位置应该包含一个非常量的
float*
,并且稍后可能会尝试更改这个指向的float
。因此,您不能将
float**
转换为const float**
,它允许您将指向常量的指针存储在需要指向可变值的指针的位置。有关更多详细信息,请参阅 C++ FAQ Lite。
If you converted the parameter to
const float**
you could then store aconst float*
at the memory location where the parameter points to. But the calling function thinks that this memory location is supposed to contain a non-constfloat*
and might later try to change this pointed-tofloat
.Therefore you cannot cast a
float**
to aconst float**
, it would allow you to store pointers to constants in locations where pointers to mutable values are expected.For more details see the C++ FAQ Lite.