11

Can anyone explain the difference between

enum 
{Type1,type2}

And

enum class
{Type1, type2}

I use the former often (probably too often without encapsulating enough) but I've never used the second example.

Thanks

enum

unknownSPY
  • 706
  • 4
  • 15
  • 27

1 Answers1

15

An enum just spills its contents into the enclosing scope, and is basically a const static integer. This means that the first element of any default enum is the same using the == operator.

Enum classes have their own scope, and don't pollute the namespace that they are in. They also make sure that the first element in any enum classes aren't equal.

Prefer enum classes because of their perks if you have a compiler that supports them (any major compiler by now)

I'd you want to learn more go here:

http://en.cppreference.com/w/cpp/language/enum

Russell Greene
  • 2,141
  • 17
  • 29