Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 10 of 441 min read6 questions

Enums

Named integer constants; prefer `enum class`.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. Enum = names for integer constants. Values start at 0 and auto-increment unless assigned (enum{A=3,B,C} → B=4, C=5).
  2. Under the hood it's just an int; set the underlying type for size: enum Color : uint8_t {...} (1 byte vs default 4).
  3. Plain enum: names leak into the enclosing scope (collisions) + implicit int conversion (silent bugs).
  4. enum class (C++11): names scoped (Color::Red), no implicit int conversion (needs static_cast). Preferred.
Watch out

Easily confused

enum vs enum class
Plain enum leaks names and converts to int implicitly. enum class is scoped and type-safe (no implicit int conversion).

Test yourself

6 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.