Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between enum and enum class [duplicate]

Tags:

c++

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

like image 506
unknownSPY Avatar asked Oct 05 '15 16:10

unknownSPY


1 Answers

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

like image 116
Russell Greene Avatar answered Sep 21 '22 17:09

Russell Greene