문제

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