Skip to content

Latest commit

 

History

History
109 lines (81 loc) · 2.52 KB

Enums.md

File metadata and controls

109 lines (81 loc) · 2.52 KB

Enums (enum)

Explanation

  1. An enum (short for "enumeration") is a user-defined type that consists of a set of named integral constants, providing a way to define a collection of related constants with meaningful names.
  2. Enumerations improve code readability by replacing numeric values with descriptive names.

Usage

  1. Represent some states with integers by using an enum to group and represent them.

Unscoped Enumeration Syntax

enum EnumName {
   MEM1,   // 0
   MEM2,   // 1
   ...,
   MEMN = n,   // n
   MEMN1,       // n + 1
   ...
};

EnumName enum_name = MEM2;

Scoped Enumeration Syntax

enum EnumName : Type {
   MEM1,   // 0
   MEM2,   // 1
   ...,
   MEMN = n,   // n
   MEMN1       // n + 1
   ...
};

EnumName enum_name = EnumName::MEM2;

Unscoped Enumeration Syntax with a Scope

Class ClassName {
   enum EnumName {
      MEM1,   // 0
      MEM2,   // 1
      ...,
      MEMN = n,   // n
      MEMN1,      // n + 1
      ...
   };
};

EnumName enum_name = ClassName::MEM2;

Differences Between enum (Unscoped Enumeration) and enum class (Scoped Enumeration)

Syntax

Namespace Scoping

  1. enum: Enum values injected into enclosing scope.
  2. enum class: Enum values scoped within the enum type.

Type Safety

  1. enum: Implicitly converts to int.
  2. enum class: No implicit conversions.

Underlying Type

  1. enum: Cannot specify underlying type explicitly.
  2. enum class: Can specify underlying type (e.g., enum class Color : unsigned int).

Forward Declaration

  1. enum: Not allowed.
  2. enum class: Allowed.

Bitwise Operations

  1. enum: Allowed without extra effort.
  2. enum class: Not allowed without operator overloading.