Create an enumeration of constants with and without explicit values.
enum fruits { apple, banana, cherry }; enum fruits { apple = 0, banana = 1, cherry = 2 };
Note that, unlike in C, you can refer to the type here as fruits
.
C++11 introduced “strongly typed enumerations”, enumerations that cannot be implicitly converted to/from integers:
enum class fruits { apple, banana, cherry }; enum class fruits { apple = 0, banana = 1, cherry = 2 };
These enumeration constants must be referred to as fruits::apple
, not just apple
.
You can explicitly specify an underlying type for the enum; the default is int
:
enum class fruits : unsigned int { apple, banana, cherry };
You can also explicitly specify an underlying type for old-style enums:
enum fruits : unsigned int { apple, banana, cherry };
Content is available under GNU Free Documentation License 1.2.