ontherocks
ontherocks

Reputation: 1999

Returning const reference from a member get method

typedef vector<int> intvec;

class MyClass
{
    intvec m_member;
public:
    const intvec& GetVec();
};

const intvec& MyClass::GetVec()
{
    return m_member;
}

int main(int argc, char *argv[])
{
    MyClass myClassObj;

    myClassObj.GetVec().push_back(11); //This doesn't work with const reference
    myClassObj.GetVec().push_back(22); //This doesn't work with const reference

    for(intvec::const_iterator iter = myClassObj.GetVec().begin(); iter != myClassObj.GetVec().end(); ++iter)
    {
        cout << *iter << ", " ;
    }

    cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

If the return type of GetVec() is non const reference then myClassObj.GetVec().push_back(11); works, but for const reference return this doesn't work (compilation error). I understand that if the GetVec() method itself was const i.e. const intvec& GetVec() const;, then myClassObj.GetVec().push_back(11); wouldn't work because that would mean modifying the "this" pointer.

Upvotes: 0

Views: 130

Answers (3)

mfnx
mfnx

Reputation: 3028

I understand that if the GetVec() method itself was const i.e. const intvec& GetVec() const;, then myClassObj.GetVec().push_back(11); wouldn't work because that would mean modifying the "this" pointer

This is correct. And a getter function should have the const which prevents you from modifying "this".

const intvec& MyClass::GetVec() const { return m_member; }

A getter function is usually used to read data. Your class (the owner of m_member) should be the one to modify that member if needed. If you can modify the member from outside the class, one looses control of who by and when m_member is being changed (making debugging a difficult process and reducing the readability for developpers).

If you want to change m_member, you would typically add a method to your class to do so.

If the return type of GetVec() is non const reference then myClassObj.GetVec().push_back(11); works, but for const reference return this doesn't work (compilation error).

You simply cannot push an element on a const vector, because then it wouldn't be a const vector :). I think you are somewhat confused about the const keyword.

const object - an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.

Upvotes: 0

kdt
kdt

Reputation: 28519

It is expected that a modifying function like push_back cannot be used on a const reference. Assuming that push_back is something that appends an item to an array, that constitutes a modification to the object, so if the object is constant then you can't do it.

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254771

You can overload the function on it's const qualification:

const intvec& GetVec() const;    // Can access, but not modify, a const object
intvec& GetVec();                // Can access or modify a non-const object

Or you could just make the data member public and have exactly the same restrictions. There's little point in accessor functions unless they're used to restrict access or maintain class invariants, which these don't.

Upvotes: 4

Related Questions