InnocentZero's Treasure Chest

HomeFeedAbout Me

18 Jun 2023

Enum Classes in C++

enum classes

Problems with regular enums:

  • Two enums cannot have the same name.
enum A {
    lmao,
    ass
};
enum B {
    lmao,
    ass
};
  • Above example, no variable can have the names lmao and ass.
  • Also, two enums of different kinds can be compared (for some reason).

Enum classes

enum class Color {
red, blue, green
};
Color color = Color::blue;

We can also specify the underlying type, egg char or int.

enum class Color : char {
    // whatever
}
Tags: programming C++

Other posts
Creative Commons License
This website by Md Isfarul Haque is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.