← Back to C++ Course | Chapter 8: OOP Core | Lesson 14 of 14

C++ Enums

What Is an Enum?

An enum defines a type whose values are restricted to a small named set, like Color { RED, GREEN, BLUE }, making code that models a fixed set of options far more readable than using raw integers with unclear meaning. Under the hood each enumerator is stored as an integer, starting at 0 by default and incrementing for each following name.

Example: What Is an Enum?

cpp
#include <iostream>

enum Color { RED, GREEN, BLUE };

int main() {
	Color c = GREEN;
	std::cout << c << std::endl;
	return 0;
}

Plain Enums and Their Pitfalls

A plain enum implicitly converts to int and its enumerator names leak into the surrounding scope, which means two plain enums in the same scope can't reuse a name like RED without a naming collision. This looseness also lets you accidentally compare or assign values from unrelated enums, since the compiler doesn't stop the implicit int conversion.

Example: Plain Enums and Their Pitfalls

cpp
#include <iostream>

enum Color { RED, GREEN, BLUE };

int main() {
	int x = RED; // implicitly converts to int
	std::cout << x << std::endl;
	return 0;
}

Enum Class for Type Safety

enum class (C++11+) scopes its enumerators inside the enum's own name (Color::RED) and does not implicitly convert to int, so mixing up two different enum types becomes a compile error instead of a silent bug. This is why modern C++ style strongly prefers enum class over the plain, unscoped enum in new code.

Example: Enum Class for Type Safety

cpp
#include <iostream>

enum class Color { RED, GREEN, BLUE };

int main() {
	Color c = Color::RED; // scoped, no implicit int conversion
	std::cout << (c == Color::RED) << std::endl;
	return 0;
}

Setting the Underlying Type

By default an enum's underlying integer type is implementation-defined, but you can pin it explicitly, e.g. enum class Status : uint8_t, to control the type's storage size, which matters when the enum is part of a struct laid out for a specific memory footprint or serialized format.

Example: Setting the Underlying Type

cpp
#include <iostream>
#include <cstdint>

enum class Status : uint8_t { Active, Inactive }; // explicitly pinned type

int main() {
	Status s = Status::Active;
	std::cout << static_cast<int>(s) << std::endl;
	return 0;
}

Enums in Switch Statements

Enums pair naturally with switch statements, since the small closed set of values makes it easy for both the programmer and the compiler to reason about every possible case; many compilers will even warn you if a switch on an enum class doesn't handle every enumerator.

Example: Enums in Switch Statements

cpp
#include <iostream>

enum class Color { RED, GREEN, BLUE };

int main() {
	Color c = Color::GREEN;
	switch (c) {
		case Color::RED: std::cout << "Red" << std::endl; break;
		case Color::GREEN: std::cout << "Green" << std::endl; break;
		case Color::BLUE: std::cout << "Blue" << std::endl; break;
	}
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.