Skip to content
  • enum & enum class · C++

enum & enum class

LearnViaNotes @learnvianotes· 2 min read

Enumerations give names to a fixed set of integral values. Prefer the scoped enum class (C++11): its names don't leak and it doesn't convert to int silently.

Unscoped enum (C-style)

C++
#include <iostream>
enum Color { Red, Green, Blue };          // Red=0, Green=1, Blue=2
enum Status { Ok = 200, NotFound = 404 };  // explicit values

int main() {
    Color c = Green;
    int n = c;                    // implicit conversion to int
    std::cout << n << ' ' << Blue + 1 << ' ' << NotFound << '\n';
}
Output
1 3 404

Scoped enum class

C++
#include <cstdint>
#include <iostream>
#include <string_view>

enum class Direction : std::uint8_t { North, East, South, West };   // underlying type: 1 byte

std::string_view name(Direction d) {
    switch (d) {
        case Direction::North: return "North";
        case Direction::East:  return "East";
        case Direction::South: return "South";
        case Direction::West:  return "West";
    }
    return "?";
}

Direction turnRight(Direction d) {
    return static_cast<Direction>((static_cast<int>(d) + 1) % 4);   // explicit conversions
}

int main() {
    Direction d = Direction::West;
    std::cout << name(d) << " -> " << name(turnRight(d)) << '\n';
    std::cout << sizeof(Direction) << ' ' << static_cast<int>(Direction::South) << '\n';
}
Output
West -> North
1 2
C++
enum class Fruit { Apple, Pear };
int main() {
    int x = Fruit::Apple;      // error: no implicit conversion to int
    return x;
}
enumenum class
Names scoped?No — RedYes — Color::Red
Implicit → int?YesNo — needs static_cast
Underlying typeImplementation-chosen (can be fixed)int by default, can be fixed (: std::uint8_t)
Forward-declarableOnly with fixed underlying typeYes

Why prefer enum class over plain enum?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report