jeudi 12 octobre 2023

Initializing enum with int in C++11

I know that we can initialize an enum with a value from its underlying type (by default, that’s int). Therefore, Red, Green and Blue is 0, 1, and 2, respectively.

enum class Color {
    Red, Green, Blue
};

My question: if an object of an enum does not equal to any instance of an enum, is it enum logically? For me, I think enum means a finite possible values. I don't understand the design choice behind it.

Color c{7};
// c not equal to any one
std::cout << (c == Color::Red) << std::endl;
std::cout << (c == Color::Green) << std::endl;
std::cout << (c == Color::Blue) << std::endl;

Aucun commentaire:

Enregistrer un commentaire