Why is my return type meaningless?

落花浮王杯 提交于 2020-01-21 06:33:07

问题


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.


回答1:


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.




回答2:


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();



回答3:


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.




回答4:


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 :)




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/686470/why-is-my-return-type-meaningless

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!