C++11 enum class

Here is an example of C++11 enum class.

#include <iostream>
using namespace std;

enum class Traffic_light { GREEN, YELLOW, RED};

Traffic_light& operator++(Traffic_light& t)
{
	switch (t)
	{
		case Traffic_light::GREEN: 
			return t = Traffic_light::YELLOW;
		case Traffic_light::YELLOW:
			return t = Traffic_light::RED;
		case Traffic_light::RED:
			return t = Traffic_light::GREEN;
	}

}

int main()
{
	Traffic_light t = Traffic_light::GREEN;
	cout<<(int)(++t)<<endl;
	cout<<(int)(++t)<<endl;
	cout<<(int)(++t)<<endl;
	return 0;
}

To compile it:

$ g++ traffic.cpp -std=c++0x

Output:

1
2
0

Leave a Reply

Your email address will not be published.

22 − = 20