This seems to do what you want:
#include <iostream>
enum class MyEnum { ALFA, BETA };
int main() {
MyEnum a = MyEnum::BETA;
switch( a ) {
case MyEnum::ALFA:
std::cout << "ALFA\n";
break;
case MyEnum::BETA:
std::cout << "BETA\n";
break;
}
}
EDIT:
As per Edward's comment, below, here goes an example of how you can have this without enum
(in my case, enum class
-- that creates a new type) or int
:
#include <iostream>
class MyEnum {
struct Alfa { constexpr operator int() const { return 0; } };
struct Beta { constexpr operator int() const { return 1; } };
int v;
public:
constexpr static Alfa ALFA {};
constexpr static Beta BETA {};
MyEnum(const MyEnum&) = default;
constexpr MyEnum(Alfa vv): v(vv) {}
constexpr MyEnum(Beta vv): v(vv) {}
constexpr MyEnum() : MyEnum(ALFA) {}
operator int () const { return v; }
};
int main() {
MyEnum a = MyEnum::BETA;
a = MyEnum::ALFA;
switch( a ) {
case MyEnum::ALFA:
std::cout << "ALFA\n";
break;
case MyEnum::BETA:
std::cout << "BETA\n";
break;
}
}
That is far more complicated (live here) :D