fork download
  1. #include <iostream>
  2.  
  3. enum Traffic_light
  4. {
  5. green,
  6. yellow,
  7. red
  8. };
  9.  
  10. // prefix increment
  11. Traffic_light& operator++(Traffic_light& t)
  12. {
  13. switch (t)
  14. {
  15. case Traffic_light::green:
  16. return t = Traffic_light::yellow;
  17. case Traffic_light::yellow:
  18. return t = Traffic_light::red;
  19. case Traffic_light::red:
  20. return t = Traffic_light::green;
  21. }
  22. }
  23.  
  24. // postfix increment
  25. Traffic_light operator++(Traffic_light& t, int)
  26. {
  27. Traffic_light tmp = t;
  28. ++t;
  29. return tmp;
  30. }
  31.  
  32. int main(int argc, const char * argv[])
  33. {
  34. Traffic_light light = Traffic_light::red;
  35. std::cout << "Light is " << light << std::endl;
  36. ++light;
  37. std::cout << "Now Light is " << light << std::endl;
  38. light++;
  39. std::cout << "And now " << light << std::endl;
  40. return 0;
  41. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Light is 2
Now Light is 0
And now 1