I am trying to overload the subscript operator ([]) on an abstract class, the function called by the overload is implemented in the concrete object.
class CollectionBase {
public:
double& operator[] (const int nIndex)
{
return getValue(nIndex);
}
virtual double getValue(int index) = 0;
};
class Collection : public CollectionBase
{
double getValue(int index) { return 0; }
};
The problem I am having is that my compiler is throwing an error on the call to getValue in the overload.
Initial value of reference to non-const must be an lvalue
Does anybody know the syntax for what I am trying to do?
The problem is that you are returning a reference to a temporary value returned from
getValue. Either make both functions returndouble&, or both returndouble.