Topic 39 of 91 7. C++ Structures and Unions Interactive Tutorial

Enumerations in C++: Defining Named Constant Values With enum

13 mins read Last updated: 26 Sep 2026 19,400 views

Direct Answer & Core Concept

Enumerations in C++ define custom types holding sets of named integral constants. Modern C++11 introduced Scoped Enumerations (enum class), which are strongly typed, do not leak their enumerator names into the enclosing namespace (requiring Scope::Enumerator access), and do not implicitly convert to integers, preventing accidental cross-enum arithmetic bugs.

Detailed Explanation & Mechanics

1. Unscoped `enum` vs Scoped `enum class`:

Architecture Diagram
ENUMERATION COMPARISON:
┌─────────────────────────┬──────────────────────────┬────────────────────────────┐
│ Feature                 │ Unscoped enum            │ Scoped enum class (C++11)  │
├─────────────────────────┼──────────────────────────┼────────────────────────────┤
│ Scope                   │ Leaks to outer namespace │ Scoped: EnumName::Value    │
│ Implicit int Conversion │ YES (Permissive/Buggy)   │ NO (Requires static_cast)  │
│ Underlying Type Control │ Implementation dependent │ Explicit (e.g. : uint8_t)  │
│ Forward Declaration     │ No                       │ Supported                  │
└─────────────────────────┴──────────────────────────┴────────────────────────────┘

Runnable Code Example: Enumerations in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <cstdint>

// Scoped enum with explicit underlying type uint8_t
enum class NetworkState : uint8_t {
    Disconnected = 0,
    Connecting = 1,
    Connected = 2,
    Error = 255
};

void checkState(NetworkState state) {
    if (state == NetworkState::Connected) {
        std::cout << "Network is healthy and connected.
";
    } else {
        std::cout << "Network state code: " << static_cast<int>(state) << "
";
    }
}

int main() {
    NetworkState current = NetworkState::Connected;
    checkState(current);

    NetworkState err = NetworkState::Error;
    checkState(err);

    std::cout << "Enum storage size: " << sizeof(current) << " byte
";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Why are enum class types preferred over traditional C-style enums in C++?

A: Scoped `enum class` types prevent namespace collisions (e.g. two enums defining `RED`), forbid accidental implicit conversion to integers, and allow explicit specification of underlying storage sizes (e.g. `: uint8_t`).
Q

Q: Can an enum class have an underlying type of int64_t?

A: Yes. Any integral type can be specified: `enum class BigId : int64_t { ... }`.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Always use `enum class` instead of unscoped `enum`. 2. Specify small underlying types (e.g. `: uint8_t`) to save memory when enumerators are small. 3. Use `static_cast(e)` when numeric serialization is necessary.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.