Menu
Enums in C++

Enums in C++

📘 C++ Enums (Enumerations)

Playlist Link: C++ Series by The Cherno
Video Reference: ENUMS in C++


1. What Are Enums?

Purpose:

  • 🏷️ Name Values: Assign meaningful names to integer values for readability.
  • 🛑 Restrict Values: Limit variables to a predefined set of options (e.g., error levels).
  • 🧩 Group Related Constants: Organize values under a single type.

Example:

// Without Enum (messy integers)  
int errorLevel = 0;  
int warningLevel = 1;  
int infoLevel = 2;  

// With Enum (clean & grouped)  
enum LogLevel {  
    Error = 0,  
    Warning = 1,  
    Info = 2  
};  

Video Timestamp: “Enums are just integers with names.”


2. Basic Enum Syntax

Declaration

enum LogLevel {  
    Error,   // Defaults to 0  
    Warning, // Defaults to 1  
    Info     // Defaults to 2  
};  
  • 🔢 Default Values: Start at 0 and increment by 1.
  • 🔄 Explicit Assignment: Override defaults:
    enum LogLevel {  
        Error = 5,    // 5  
        Warning,      // 6  
        Info = 10     // 10  
    };  
    

Usage

LogLevel currentLevel = LogLevel::Error;  

Video Timestamp: “Enums restrict values to predefined options.”


3. Specifying Underlying Type

Optimize Memory:

  • Use smaller integer types (e.g., unsigned char for 0-255 values).
    enum LogLevel : unsigned char {  
      Error,   // 0 (1 byte)  
      Warning, // 1  
      Info     // 2  
    };  
    
  • 🚫 No Floats/Other Types: Underlying type must be an integer.

4. Real-World Example: Log Class

Before Enum (Using Integers)

class Log {  
private:  
    int m_LogLevel = 0; // 0=Error, 1=Warning, 2=Info  
public:  
    void SetLevel(int level) {  
        m_LogLevel = level;  
    }  
};  

Issue: m_LogLevel can be set to invalid values (e.g., 5).

After Enum (Type-Safe)

class Log {  
public:  
    enum Level {  
        LevelError = 0,  
        LevelWarning,  
        LevelInfo  
    };  

private:  
    Level m_LogLevel = LevelError;  

public:  
    void SetLevel(Level level) {  
        m_LogLevel = level;  
    }  
};  

Usage:

Log logger;  
logger.SetLevel(Log::LevelWarning); // ✅ Valid  
// logger.SetLevel(5); ❌ Compile error!  

Video Timestamp: “Enums make code self-documenting.”


5. Benefits of Enums

  • 🧠 Readability: LogLevel::Error > 0.
  • 🛡️ Type Safety: Prevents invalid assignments.
  • 📦 Encapsulation: Groups related constants.

6. Common Pitfalls

Name Conflicts

enum LogLevel { Error, Warning, Info };  
void Error() { /* ... */ } // ❌ Compiler error: name clash!  

Fix: Prefix enum values:

enum LogLevel {  
    LevelError,  
    LevelWarning,  
    LevelInfo  
};  

Implicit Integer Conversion

LogLevel level = LogLevel::Error;  
int value = level; // ✅ Allowed (Error = 0)  

Video Timestamp: “Enums are just integers under the hood.”


7. Enum Classes (Scoped Enums)

Difference from Plain Enums:

  • 🎯 Strongly Typed: No implicit conversion to integers.
  • 🔒 Scope: Values are under the enum’s namespace.
    enum class LogLevel { Error, Warning, Info };  
    LogLevel level = LogLevel::Error;  
    // int value = level; ❌ Not allowed!  
    

    Covered in-depth in a future video.


📼 Video Summary

  • Enums vs. Integers: Enums add clarity and safety (Timestamp).
  • Log Class Example: Transition from integers to enums (Timestamp).
  • Best Practices: Prefix values to avoid name conflicts.

Next Steps: Explore enum class for stricter type control! 🚀
Full Video: ENUMS in C++