Reputation: 31
I'm trying to overload the indexing operator []
. I have two versions
and I need to use both of them, but when I build the project one of them is called and I get an error wherever I use the second one: Invalid operands to binary expression double and Quarters
.
Here's the first one:
Quarters& Security :: operator [] (QuarterType quarter){
return quartersData[static_cast<int>(quarter)];
}
and the second one:
const double& Security :: operator [] (QuarterType quarter) const{
return (quartersData[static_cast<int>(quarter)].getPrediction());
}
What is the problem and what can I do to fix it?
Upvotes: 0
Views: 116
Reputation: 41331
Overriding doesn't take into account the return type. Since in both cases the parameter types are the same, overriding takes into account only const
-ness of the object. If your object is not const
, then non-const version of operator[]
will be called, regardless of what you are trying to do next with it.
Upvotes: 2