Difference Between Enum and Enum Class [Duplicate]

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

1

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:

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest