Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Data Types

Enum

An enum, short for enumeration, is a user-defined data type in C++ used to assign names to integral constants. Enums make the code more readable and maintainable by giving meaningful names to a set of related values.

There are two types of enums in C++:

  1. Traditional C-style enums (introduced in C and carried over to C++)

  2. Enum classes (introduced in C++11)

Let's look at both:

  1. Traditional C-style enums:

enum Color {
    RED,    // 0
    GREEN,  // 1
    BLUE    // 2
};

Color myColor = GREEN;

Key points:

  • By default, the first enumerator is assigned the value 0, and each subsequent enumerator is incremented by 1.

  • You can assign specific values to enumerators:

    enum Color {
        RED = 5,
        GREEN = 10,
        BLUE = 15
    };
  • Enumerators are in the same scope as the enum, which can lead to name conflicts.

  • They implicitly convert to integers.

  1. Enum classes (C++11 and later):

enum class Fruit : unsigned int {
    APPLE,
    BANANA,
    ORANGE
};

Fruit myFruit = Fruit::BANANA;

Key points:

  • Enum classes provide stronger type safety.

  • They don't implicitly convert to integers.

  • Enumerators are scoped within the enum class.

  • You can specify the underlying type (e.g., unsigned int).

Here's a more comprehensive example demonstrating both types:

#include <iostream>

// Traditional enum
enum Color {
    RED,
    GREEN,
    BLUE
};

// Enum class
enum class Direction : char {
    NORTH = 'N',
    EAST = 'E',
    SOUTH = 'S',
    WEST = 'W'
};

int main() {
    // Using traditional enum
    Color paint = GREEN;
    int colorCode = paint;  // Implicit conversion to int
    std::cout << "Color code: " << colorCode << std::endl;

    // Using enum class
    Direction way = Direction::EAST;
    // std::cout << way; // This would not compile
    std::cout << "Direction: " << static_cast<char>(way) << std::endl;

    // Comparison
    if (paint == GREEN) {
        std::cout << "The paint is green." << std::endl;
    }

    if (way == Direction::EAST) {
        std::cout << "We're heading east." << std::endl;
    }

    return 0;
}

Benefits of using enums:

  1. Improved code readability

  2. Type safety (especially with enum classes)

  3. Easy to maintain and modify related constants

Enum classes are generally preferred in modern C++ due to their stronger type safety and scoping rules. However, traditional enums are still widely used, especially in code bases that need to maintain compatibility with C or older C++ standards.

PreviousData TypesNextTypeDef

Last updated 9 months ago

Try it .

🪅
here