1

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?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • It isn't related to classes. Don't be fooled by the poor choice of name. It's just a safer enum. – R. Martinho Fernandes Feb 06 '13 at 15:55
  • `enum class` is just an enum, and not like a class at all. – Zyx 2000 Feb 06 '13 at 15:55
  • 1
    I'd be hard pressed to beat [the descriptions here](http://en.cppreference.com/w/cpp/language/enum) as a place to start. – WhozCraig Feb 06 '13 at 15:57
  • [This document](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf) is one of the draft proposals for `enum class` I found with a quick web search. It doesn't say much about the name choice, but does talk about ways people work around some of the problems with traditional `enum`s by wrapping them in a `class`. There are phrases in there like `Like a class, the new enum type introduces its own scope` which expose some of the thinking behind it. – BoBTFish Feb 06 '13 at 16:11

1 Answers1

3

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
Pete Becker
  • 74,985
  • 8
  • 76
  • 165