Language: EN

cpp-enumeraciones

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

In C++ an enumeration (or enum) is a way to define a set of named integer values under the same 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 make code easier to read and maintain by replacing magic numbers with descriptive names.

Definition of an Enumeration

To define an enumeration in C++, the enum keyword is used, followed by the name of the enumeration and the members in braces.

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

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 the members of an enumeration (instead of using the default values).

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

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

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

Use of enumerations

Assigning values

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

DiasDeLaSemana hoy = Lunes;

Comparing values

You can also compare these values using equality operators:

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

Conversion of enumerations

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

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)

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 for creating a scoped enumeration

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

For example,

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

Color c = Color::Rojo;

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

:::::::