为什么编译器会警告返回类型上的 const 没有意义?
我正在尝试使用 const MyClass * const
的返回类型。 但是,我收到警告:
警告:#815-D:返回类型上的类型限定符毫无意义。
这不是一个有效的类型吗? 我想要一个不能更改的指针,并且我希望它指向的东西也不能被更改。
I am trying to use a return type of const MyClass * const
. However, I get a warning:
Warning: #815-D: type qualifier on return type is meaningless.
Is this not a valid type? I want a pointer than cannot be changed, and I want the thing it points to to not be changed either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
指针本身具有值类型,因此将其设置为 const 是没有意义的。 调用函数对返回值执行的操作不受被调用函数的限制。 这类似于尝试定义类似的东西:
getInt(),在这种情况下,只返回一个 int 值(而不是引用)。 它进入寄存器,然后调用者函数接收它并用它做任何它想做的事。
The pointer itself has value type, so it doesn't make sense to make it const. What the caller function does with the returned value can't be restricted by the called function. This is akin to trying to define something like:
getInt(), in this case, just returns an int value (not a reference). It goes to a register, then the caller function receives it and does whatever it wants with it.
我同意朱利亚诺的回答,你希望指针的常量位于调用站点。
完成您正在寻找的操作的更好方法是让您的函数返回对该对象的 const 引用:
根据定义,引用不能被修改,所以您会更好。
当然,如果您的函数尝试创建 MyClass 对象,则这将不起作用。 在这种情况下,您只需将额外的常量移至调用站点即可。
I agree with Juliano's answer, you want the constness of the pointer to be at the call site.
A better way to do what you are looking for is to have your function return a const reference to the object:
By definition references can't be modified, so you'd be better off.
Of course this won't work if your function is attempting to create a MyClass object. In that case you can just move the extra const to the call site.
为什么要关心指针是否改变? 这样做就像在说:
f() 返回的值是一个副本 - 更改它不会改变任何内容,因此将其设为 const 没有任何意义。
Why do you care if the pointer is changed? Doing this is like saying:
The value returned by f() is a copy - changing it changes nothing, so there is nom point in making it const.
确保定义所需返回类型的一种简单方法是始终在原始类型的右侧(而不是左侧)添加修饰符。
这应该有助于确保您的返回类型不再毫无意义:)
One simple way of making sure you're defining the return type you want is to always add modifiers on the right (as opposed to left) side of original type.
That should help make sure your return types are never meaningless again :)
const 限定符没有任何意义,因为你返回的是一个指针
the const qualifier doesn't mean anything because you're returning a pointer
为什么返回 const 值? 考虑以下内容(请原谅缺乏想象力的变量名称):
鉴于
operator+
的声明,我可以执行以下操作:没错,我刚刚分配给临时
A
由operator+
返回。da
的结果值为 4,而不是 5。将operator+
的返回类型更改为const A
可阻止此赋值,从而导致表达式(a+b) = c
生成编译器错误。如果我尝试分配给从函数返回的指针或整数,我的编译器 (MSVC) 会生成“左操作数必须是左值”错误,这似乎与 ARM 编译器告诉您指针的常量性毫无意义是一致的--无论如何,指针都不能被赋值。 但对于类/结构,显然可以分配给非常量返回值。
Why return a const value? Consider the following (and please excuse the unimaginative variable names):
Given that declaration of
operator+
, I can do the following:That's right, I just assigned to the temporary
A
that was returned byoperator+
. The resulting value ofd.a
is 4, not 5. Changing the return type ofoperator+
toconst A
prevents this assignment, causing the expression(a+b) = c
to generate a compiler error.If I try to assign to a pointer or integer returned from a function, my compiler (MSVC) generates a "left operand must be l-value" error, which seems consistent with the ARM compiler telling you that the constness of the pointer is meaningless--the pointer can't be assigned to anyway. But for classes/structs, apparently it's okay to assign to non-const return values.