TableOfContents


Overloading operator++ for enums

   1 enum Triple {
   2     ALPHA,
   3     BETA,
   4     THETA,
   5     NUM_TRIPLES,    //this is equal to the number of elements in the enum
   6 };
   7 
   8 // prefix operator++
   9 Triple& operator++( Triple& triple ) {
  10 
  11     return triple = static_cast< Triple >( (triple + 1) % NUM_TRIPLES );
  12 
  13 }
  14 
  15 int main() {
  16 
  17     Triple triple = ALPHA;
  18     ++triple; // you can now do this and it'll wrap around at 3
  19 
  20 }


yawn, I'll add more later, need sleep now

EnumTricks (last edited 2007-05-06 18:28:59 by pool-71-127-211-44)