Suppose I have some class X
which I return from a method:
class A
{
X GetValue();
void SetValue(X x);
// X& GetValue(); // old code
private:
X x;
};
In the past I used to return by reference instead of using setters and getters, but I've opted for the latter because the class is so small, so now my code might have occurrences of this kind of thing happening:
instanceOfA.GetValue() = something;
This is ofcourse complete nonsense now, and I want to disallow it. What can I do to prevent X
being assigned to in this way? I thought about returning const X
from my GetValue()
method, but I'm told that doing this is obsolete.