C++ Core
Enums
Named integer constants; prefer `enum class`.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- Enum = names for integer constants. Values start at 0 and auto-increment unless assigned (
enum{A=3,B,C}→ B=4, C=5). - Under the hood it's just an int; set the underlying type for size:
enum Color : uint8_t {...}(1 byte vs default 4). - Plain
enum: names leak into the enclosing scope (collisions) + implicit int conversion (silent bugs). enum class(C++11): names scoped (Color::Red), no implicit int conversion (needsstatic_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.