(inspired by LinkedIn post)
C has had enum since C89. C++ inherited C enum. C/C++ enum is not type safe. C++ 11 added enum class.
Primarily the differences are:
They share:
I think that in most cases enum class should be preferred to old style enum.
Example code:
#include <iostream>
using namespace std;
enum E1 { A, B, C };
enum E2 : short { D, E, F };
enum E3 { G=1, H=2, I=4 };
enum class E4 { J, K, L };
enum class E5 : short { M, N, O };
enum class E6 { P=1, Q=2, R=4 };
int main()
{
E1 e1 = B;
cout << e1 << " (" << sizeof(e1) << ")" << endl;
E2 e2 = E;
cout << e2 << " (" << sizeof(e2) << ")" << endl;
E3 e3 = H;
cout << e3 << " (" << sizeof(e3) << ")" << endl;
cout << "avg = " << ((e1 + e2 + e3) / 3) << endl;
E4 e4 = E4::K;
cout << (int)e4 << " (" << sizeof(e4) << ")" << endl;
E5 e5 = E5::N;
cout << (int)e5 << " (" << sizeof(e5) << ")" << endl;
E6 e6 = E6::Q;
cout << (int)e6 << " (" << sizeof(e6) << ")" << endl;
//cout << "avg = " << ((e4 + e5 + e6) / 3) << endl;
cout << "avg = " << ((static_cast<int>(e4) + static_cast<int>(e5) + static_cast<int>(e6)) / 3) << endl;
return 0;
}