Search code examples
c++stdvectormember-functionsconst-correctnessfunction-qualifier

How can you return a non-const reference to an element in a std::vector data member from a const member function?


I'm having a hard time figuring out how to return a non-const reference to an element in a std::vector from a const member function. A simple example of what I'm going for is,

template<class T>
class MyClass
{
 public:
 MyClass : myVec(3)
 {
 }

 T& x() const
 {
    return *(myVec.data())[0]
 }

 std::vector<T> myVec;
}

The behavior I'm going for is that I'd like to be able to do things like the following,

MyClass obj<double>;
obj.x() = 3.3;
assert(obj.x()==3.3)

Eigen gives the same type of behavior, but I've not been able to figure out how to get it to work.


Solution

  • You could use const_cast, and it'll do the work here, (according to the usage posted). But you won't need it for this case (and for most cases).

    You could (and should) add non-const member function overloading for it. Then const member function returns reference to const, non-const member function returns reference to non-const. The appropriate one will be invoked via overload resolution.

    const T& x() const
    {
       return myVec[0];
    }
    T& x()
    {
       return myVec[0];
    }