From the course: C++ Best Practices for Developers

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Strongly typed enums

Strongly typed enums - C++ Tutorial

From the course: C++ Best Practices for Developers

Start my 1-month free trial

Strongly typed enums

- [Man] Enum classes are strongly typed, unlike traditional enums, and they don't pollute the surrounding scope. Redefinition of enumerators is no longer an issue as well. Implicit conversions are not allowed with enum class. Prefer enum class to traditional enums. Let's take a look at a code sample. Traditional enums are weakly typed, so we can't have two enumerators with the same definition in the same name space. So here we have a traffic light with colors red, green, and yellow. Here we have a car color with black, white, gray, and silver but look what happens once soon as I comment in green, we notice that we get the error right away. Redefinition of enumerator green and it's saying that hey, I've got it defined here and you can't also have it defined here. And that's kind of a problem. The enum class has no problem with enumerators of the same name. Dog is defined in both pet and mammal. So here's dog in pet. Here's dog in mammal. And it's not a problem. Now you notice that even…

Contents