C++ › Enums & Enum Classes

How do you assign specific values to enumerators?

Beginner

Answer

You can assign explicit integer values to enumerators; unassigned ones continue incrementing from the previous value. This is used to match external codes, create bit flags (powers of two), or leave gaps. Enumerators without values start at 0 and increase by 1.
enum class Http { OK = 200, NotFound = 404, Error = 500 };
Real-world example HTTP status codes are modeled as an enum with their real numeric values for clarity.

Common follow-ups: What value does an unassigned enumerator take? | Why assign explicit values?