Why is my return type meaningless?

后端 未结 6 693
别跟我提以往
别跟我提以往 2021-01-11 16:05

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 m

相关标签:
6条回答
  • 2021-01-11 16:28

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

    0 讨论(0)
  • 2021-01-11 16:30

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

    0 讨论(0)
  • 2021-01-11 16:33

    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.

    0 讨论(0)
  • 2021-01-11 16:34

    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();
    
    0 讨论(0)
  • 2021-01-11 16:40

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题