Is the type enum class a completely separate from a traditional class, or is its implementation similar? How does enum class work? The reason I ask is because I don't understand how it can be similar to both a class and an enum at the same time. I assume an enum class cannot have a constructor, or internal methods?

有帮助吗?

解决方案

It's related to an ordinary enum in that it consists of a set of names for constant values. It's related to a class in that the names are all contained within the scope of the type's name. So:

enum my_enum {
    first,
    second,
    third
};

my_enum value = second; // OK; enumeration names are in global scope here

enum class my_class_enum {
    fourth,
    fifth,
    sixth
};

my_class_enum other_value = fourth; // Error
my_class_enum another_value = my_class_enum::fourth; // OK
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top