This code below shows how to overload the <<
operator for an enum type. (Taken from here).
days operator+ (days d)
{
return static_cast<days>((static_cast<int>(d) + 1) % 7);
}
ostream& operator<< (ostream& out, days d)
{
switch(d)
{
case SUNDAY: out << "SUNDAY";
break;
case MONDAY: out << "MONDAY";
break;
case TUESDAY: out << "TUESDAY";
break;
case WEDNESDAY: out << "WEDNESDAY";
break;
case THURSDAY: out << "THURSDAY";
break;
case FRIDAY: out << "FRIDAY";
break;
case SATURDAY: out << "SATURDAY";
break;
}
return out;
}
The above code can ostensibly be used in the following way:
int main()
{
days aDay = SUNDAY;
cout << +aDay << endl;
return 0;
}
It is clear that the intent here is to overload the <<
operator, for use on the enumurated type days
. Eventually, we will use it as: cout << aDay << endl;
What I do not understand, is how/where we are inputing the out
stream. I can understand that a day
object is being input into the <<
since its on the right side, but I do not see where the out
object is being input... there is only one thing on the right hand side here...
Thank you.