Enum in C ++


What are Enums?
Enums are a way to create a set of named integer constants.  They make your code more readable and maintainable by replacing magic numbers with meaningful names.  Think of them as a way to define a type that can only hold one of a limited set of values.
Basic Enum Syntax
enum class Color {
  Red,
  Green,
  Blue,
  // ... more colors
};

// Usage:
Color myColor = Color::Blue;

if (myColor == Color::Green) {
  // ... do something
}

Explanation:
 * enum class Color: This declares an enum named Color.  The class keyword makes this a scoped enum (more on this below).
 * Red, Green, Blue: These are the enumerators.  They are the possible values that a variable of type Color can hold.  By default, the first enumerator is assigned the value 0, the next 1, and so on.
 * Color myColor = Color::Blue;: This declares a variable myColor of type Color and initializes it to the value Blue.  Note the use of Color::Blue because it's a scoped enum.
Scoped Enums (enum class)
Using enum class (scoped enums) is strongly recommended in modern C++.  They provide several advantages:
 * Strong Typing:  Scoped enums are strongly typed.  This means you can't implicitly convert them to integers or other enum types.  This prevents accidental mixing of different enum types.
 * Scope:  The enumerators are scoped within the enum.  This means you can have enumerators with the same name in different enums without conflicts.  You access them using the EnumName::EnumeratorName syntax (e.g., Color::Red).
 * No Implicit Conversions:  As mentioned, scoped enums don't implicitly convert to integers.  If you need an integer value, you must explicitly cast (e.g., static_cast<int>(myColor)).
Unscoped Enums (enum)
Older code might use the enum keyword without class.  These are unscoped enums.  While they still work, they have some drawbacks:
 * Weak Typing:  Unscoped enums implicitly convert to integers.  This can lead to errors if you accidentally mix different enum types or use an integer where an enum is expected.
 * No Scope:  The enumerators are in the surrounding scope.  This can cause name collisions if you have enumerators with the same name in different enums.
// Unscoped enum (less recommended)
enum Fruit {
  Apple,
  Banana,
  Orange
};

Fruit myFruit = Banana; // No scope needed here.

int fruitValue = myFruit; // Implicit conversion to int (value will be 1).

Specifying Enumerator Values
You can explicitly set the values of enumerators:
enum class StatusCode {
  OK = 200,
  NotFound = 404,
  InternalServerError = 500
};

Underlying Type
By default, the underlying type of an enum is int.  You can specify a different underlying type:
enum class SmallNumber : unsigned char {
  One = 1,
  Two = 2,
  Three = 3
};

Example: State Machine
Enums are often used to represent states in a state machine:
enum class State {
  Idle,
  Running,
  Paused,
  Stopped
};

State currentState = State::Idle;

// ... later in your code ...
if (currentState == State::Running) {
  // ... do something
}

Key Differences: Scoped vs. Unscoped Enums
| Feature | Scoped Enum (enum class) | Unscoped Enum (enum) |
|---|---|---|
| Typing | Strong | Weak |
| Scope | Enumerators are scoped | Enumerators are not scoped |
| Conversions | No implicit conversions | Implicit conversions to int |
| Best Practice | Recommended | Less recommended |
In summary:  Always prefer scoped enums (enum class) in modern C++ for their type safety and scoping benefits.  They make your code more robust and easier to maintain.  Use unscoped enums only if you're working with legacy code that requires them.

Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Kotlin Strings: Features and Operations Guide

Kotlin Android Program (QCR) Application Codes That Read Text in Photos