为什么编译器会警告返回类型上的 const 没有意义?

发布于 2024-07-15 06:59:49 字数 192 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别挽留 2024-07-22 06:59:49

指针本身具有值类型,因此将其设置为 const 是没有意义的。 调用函数对返回值执行的操作不受被调用函数的限制。 这类似于尝试定义类似的东西:

const int getInt();

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:

const int getInt();

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.

古镇旧梦 2024-07-22 06:59:49

我同意朱利亚诺的回答,你希望指针的常量位于调用站点。

完成您正在寻找的操作的更好方法是让您的函数返回对该对象的 const 引用:

const MyClass& getMyClass()
{ 
  return myClass;
}

根据定义,引用不能被修改,所以您会更好。

当然,如果您的函数尝试创建 MyClass 对象,则这将不起作用。 在这种情况下,您只需将额外的常量移至调用站点即可。

// function definition
const MyClass* createMyClass()
{ 
  return new MyClass("I Love C++");
}

// use of a const pointer to a MyClass which is const
const MyClass* const myClassInstance = creatMyClass();

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:

const MyClass& getMyClass()
{ 
  return myClass;
}

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.

// function definition
const MyClass* createMyClass()
{ 
  return new MyClass("I Love C++");
}

// use of a const pointer to a MyClass which is const
const MyClass* const myClassInstance = creatMyClass();
夏至、离别 2024-07-22 06:59:49

为什么要关心指针是否改变? 这样做就像在说:

const int f() {
   ...
}

f() 返回的值是一个副本 - 更改它不会改变任何内容,因此将其设为 const 没有任何意义。

Why do you care if the pointer is changed? Doing this is like saying:

const int f() {
   ...
}

The value returned by f() is a copy - changing it changes nothing, so there is nom point in making it const.

听风吹 2024-07-22 06:59:49

确保定义所需返回类型的一种简单方法是始终在原始类型的右侧(而不是左侧)添加修饰符。

MyClass                 // MyClass object
MyClass const           // MyClass object which can't be modified
MyClass const &         // reference of an unmodifiable MyClass object 
MyClass const *         // pointer to an unmodifiable MyClass object
MyClass const * const   // unmodifiable pointer to an unmodifiable MyClass object
MyClass const * const & // reference of an unmodifiable pointer to an unmodifiable MyClass object

这应该有助于确保您的返回类型不再毫无意义:)

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.

MyClass                 // MyClass object
MyClass const           // MyClass object which can't be modified
MyClass const &         // reference of an unmodifiable MyClass object 
MyClass const *         // pointer to an unmodifiable MyClass object
MyClass const * const   // unmodifiable pointer to an unmodifiable MyClass object
MyClass const * const & // reference of an unmodifiable pointer to an unmodifiable MyClass object

That should help make sure your return types are never meaningless again :)

ま柒月 2024-07-22 06:59:49

const 限定符没有任何意义,因为你返回的是一个指针

the const qualifier doesn't mean anything because you're returning a pointer

╰沐子 2024-07-22 06:59:49

为什么返回 const 值? 考虑以下内容(请原谅缺乏想象力的变量名称):

struct A { int a; };

A operator+(const A& a1, const A& a2) 
{
    A a3 = { a1.a + a2.a };
    return a3;
}

鉴于 operator+ 的声明,我可以执行以下操作:

A a = {2}, b = {3}, c = {4}, d = ((a+b) = c);

没错,我刚刚分配给临时 Aoperator+返回。 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):

struct A { int a; };

A operator+(const A& a1, const A& a2) 
{
    A a3 = { a1.a + a2.a };
    return a3;
}

Given that declaration of operator+, I can do the following:

A a = {2}, b = {3}, c = {4}, d = ((a+b) = c);

That's right, I just assigned to the temporary A that was returned by operator+. The resulting value of d.a is 4, not 5. Changing the return type of operator+ to const 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文