Skip to content

Instantly share code, notes, and snippets.

@satishgoda
Last active January 1, 2016 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishgoda/8209935 to your computer and use it in GitHub Desktop.
Save satishgoda/8209935 to your computer and use it in GitHub Desktop.
Enumeration Classes
#include <iostream>
#include <string>
using namespace std;
enum class TrafficLight { green, yellow, red };
TrafficLight& operator++(TrafficLight& light) {
switch (light) {
case TrafficLight::green:
return light = TrafficLight::yellow;
case TrafficLight::yellow:
return light = TrafficLight::red;
case TrafficLight::red:
return light = TrafficLight::green;
}
}
ostream& operator<<(ostream& out, const TrafficLight& light)
{
static const vector<string> names {"green", "yellow", "red"};
return out << names[static_cast<string::size_type>(light)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment