cpp-enumeraciones

What are enumerations and how to use them in C++

  • 4 min

In C++, an enumeration (or enum) is a way to define a set of named integer values under a single type.

Each member of an enumeration is a constant that represents a unique integer value (starting at 0 by default and increasing by one for each subsequent member).

Enumerations improve code readability and maintainability by replacing magic numbers with descriptive names.

If you want to learn more, check out the Introduction to Programming Course

Defining an Enumeration

To define an enumeration in C++, use the keyword enum, followed by the enumeration name and its members within braces.

enum DiasDeLaSemana
{
    Lunes,
    Martes,
    Miércoles,
    Jueves,
    Viernes,
    Sábado,
    Domingo
};
Copied!

In this example,

  • DiasDeLaSemana is an enumeration with seven members: Lunes, Martes, Miércoles, Jueves, Viernes, Sábado, and Domingo.
  • By default, Lunes has the value 0, Martes has the value 1 (and so on).

Enumerations with specific values

It is possible to assign specific values to enumeration members (instead of using the default values).

enum DiasSemana
{
    Lunes = 1,
    Martes = 2,
    Miércoles = 3,
    Jueves = 4,
    Viernes = 5,
    Sábado = 6,
    Domingo = 7
};
Copied!

If the numbers are sequential, you can number only the first one (the rest will be assigned automatically in sequential order).

enum DiasSemana
{
    Lunes = 1,
    Martes,
    Miércoles,
    Jueves,
    Viernes,
    Sábado,
    Domingo
};
Copied!

Using Enumerations

Value Assignment

Once an enumeration is defined, you can assign values to variables of the enumeration type:

DiasDeLaSemana hoy = Lunes;
Copied!

Value Comparison

You can also compare these values using equality operators:

if (hoy == Lunes)
{
    std::cout << "Today is Monday." << std::endl;
}
Copied!

Enumeration Conversion

Enumerations in C++ are based on integer data types. Therefore, it is possible to convert between an enumeration and its underlying type (which by default is int).

You can convert an enumeration value to its integer representation as follows:

int valorNumerico = static_cast<int>(Miércoles);
std::cout << valorNumerico << std::endl; // Output: 2
Copied!

You can also convert an integer to an enumeration, as long as the value is valid for the enumeration:

DiasDeLaSemana dia = static_cast<DiasDeLaSemana>(4);
std::cout << dia << std::endl; // Output: 4 (depending on the implementation, it may require an additional cast to print the name)
Copied!

Scoped Enumerations

Scoped enumerations (enum class or enum struct) are a safer way to define enumerations in C++, as the values of a scoped enumeration do not implicitly convert to integers and do not enter the global scope.

The syntax to create a scoped enumeration:

enum class NombreEnum : tipo_base {
    Valor1,
    Valor2,
    // More values
};
Copied!

For example,

enum class Color : char {
    Rojo,
    Verde,
    Azul
};

Color c = Color::Rojo;
Copied!

In this example,

  • Color is a scoped enumeration with the values Rojo, Verde, and Azul.
  • Each value is associated with a char type.

Practical Examples