The Week class below is essentially a collection of Day objects:
struct Day {
//.. data members
};
enum Weekday { Sun = 0, Mon, Tue, Wed, Thu, Fri, Sat };
struct Week {
// overloaded [] operator
const Day& operator[](Weekday wd) const {
if(wd < Sun || wd > Sat)
throw std::out_of_range("Weekday out of range");
return days[wd];
}
// .. more methods
private:
Day days[7];
};
The above implementation of operator []( )
is good for only reading the Day elements in Week, but it is not possible to modify a Day in the Week through the subscript operator:
Week wk;
//.....
Day d = wk[Sun]; // OK
wk[Mon] = Day(); // ERROR!!!
We want to change the declaration of operator [](), without changing its implementation, so that the Week's Day elements can be modified. Select the appropriate declaration that would work: