Why is my return type meaningless?

2020-03-12 04:18发布

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.

6条回答
做自己的国王
2楼-- · 2020-03-12 04:28

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.

查看更多
女痞
3楼-- · 2020-03-12 04:29

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.

查看更多
该账号已被封号
4楼-- · 2020-03-12 04:31

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

查看更多
你好瞎i
5楼-- · 2020-03-12 04:39

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.

查看更多
疯言疯语
6楼-- · 2020-03-12 04:40

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();
查看更多
在下西门庆
7楼-- · 2020-03-12 04:41

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

查看更多
登录 后发表回答