Overloading Operators for enum Types

For some enum types, it may be useful to define overloaded operators, such as ++ and –, that can iterate through the enumerator values:

 #include using namespace std;enum Days {Mon, Tue, Wed, Thur, Fri, Sat, Sun};Days& operator++(Days& d, int)  // int denotes postfix++{  if (d == Sun) return d = Mon; //rollover  int temp = d;  return d = static_cast (++temp); }int main(){ Days day = Mon; for (;;) //display days as integers {  cout<< day <
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: