I want to use enumerations in a scoped format but be able to do comparison and assignment between the enumeration and unsigned integers.
This is what I have tried the code below, which works as expected:
class SystemEvents {
public:
enum {
Opened, Closed
};
};
class OtherEvents {
public:
enum {
Closed,Opened
};
};
//test
uint32_t e = SystemEvents::Opened;
if(e == OtherEvents::Closed) std::cout << "enums are weakly typed and scoped";
But I want to know if there is a way of doing it with the C++11 syntax?
enum class SystemEvents : uint32_t {
Opened,Closed
};
enum class OtherEvents : uint32_t {
Closed,Opened
};
//test
uint32_t e = SystemEvents::Opened;
if(e == OtherEvents::Closed) std::cout << "enums are weakly typed and scoped";
The code above gives me error as expected Cannot initialize a variable of type int with an rvalue of type SystemEvents
. So, Should I stick with the C style scoped enumerations or is there a way of doing it in C++11? Or is there some other way of doing this?