What is C++ enum?

In C++, enum introduces various behaviors in the same interface. Enum, short for enumeration, is essentially a group of values or a mechanism to give a value a name; rather than having a bunch of integers labelled A, B, C, we may have an enum with the values A, B, C, which correspond to integers. We may use it to pass parameters that determine a specific aspect of however we like a function to operate.

C++ enum Usage and Examples

Enums also assist us in defining a collection of values, such that rather than having an integer as a type with any value attached to it, you may limit what values can be assigned. Enums are handy when you want to utilize numbers to represent specific states or values, but you want to give them a label so your program is more legible.

Let’s have a look at an example…

Assume there are three values to be considered; we may arrange them as follows.

C++ enum Examples

We can now assign either of these values afterward in the code and use it to test if anything has to be done.

C++ enum Usage

While this is a viable option, there are some drawbacks. Firstly, the First, Second, and Third values are still not grouped, so you may unintentionally declare the Fourth variable someplace else in your code, or second, you could reassign one of the values at a later point in your code.

Using an enum to aggregate the values and control what they may be set to might be a better method to manage this problem. This would look like this:

Using an enum to aggregate values

By standard, the first is set to 0, the second is set to 1, and the third is set to 2. These values can be changed to a specific value like this:

Alternatively, if only the first number is supplied, the rest of the values will simply increase.

So, this is what the rest of our code will now look like:

Because the variable location is of the Enum Place type, it may only be allocated to the first, second, or third positions.

After everything, an enum is really just an integer; but it takes on a distinct appearance in the code to aid in keeping it clearer.

The Size of enum in C++

The size of an enum is not specified in the standards. The size of an enum is determined by the size of the base integral type that can carry the largest enumerated value, generally starting with int(4bytes); if int cannot store the values, the compiler picks a larger type.

For instance, if you declare…

Size of enum in C++

It will be a size of 8.

You can define a fixed base integral type in C++11. In most circumstances, the Char type still needs 4 bytes, and 4 bytes are highly optimized in most architectures.

Categories of C++ Enums

Scoped enums and based enums are two new enum types introduced in C++11. When referring to enumerators of a scoped enum from an enclosing scope, use a qualified term like enumname::enumerator. Furthermore, because scoped enums are strongly-typed, you would need to have an explicit cast to turn them to int.

A based enum allows you to programmatically provide its underlying type. GCC 4.4, Intel’s C++ 12, MSVC 11, IBM’s XLC++ 12.1, Clang 2.9, Embarcadero C++ Builder, and others adopt the new C++11 enum features in terms of compiler support.

Why enums are used in C++ programming?

An enum variable accepts just one value from a set of several. As an illustration, consider the following:

Why enums are used in C++ programming?

Output

It’s due to the fact that an integer’s size is 4 bytes.

Enum is a suitable choice for working with flags because of this. C++ structures may be used to accomplish the same goal. Working with enums, on the other hand, provides both efficiency and versatility.

Strongly-typed enumerations

Strongly-typed enumerations must adhere to stricter rules:

  1. Only the enumerators in the scope of the enumeration can be accessed.
  2. The enumerators do not convert to int implicitly.
  3. In the enclosing scope, the enumerators aren’t imported.
  4. The enumerators’ type is int by default. As a result, you may move the enumeration onward.

The change in syntax between conventional enumerations and strongly-typed enumerations is negligible. The keyword class or struct is added to strongly-typed enumerations.

If you wish to treat an enumerator as an int, you must use static cast to do so explicitly, as illustrated below:

You must transform the enumerators into integral types before you may compute or output them. It is possible to define either the input or even the output of strongly-typed enumerations.

How do you declare an enum in C++?

When the real-world item being modelled has extremely basic, very discrete values, an enumeration (enum) is typically utilized. The first thing that comes to mind is a traffic light, which can be in one of three states: red, yellow, or green. Let’s make a simple example utilizing the traffic light metaphor to show how enumerations (enum) function may be utilized in your application in the stages that follow.

Real-life C++ Enum Program Example:

How to declare an enum in C++

Testing the EnumerationClass

  • To test enumerations(enum) and ensure that it is working properly, add the following code:

This code might simply be separated into its own file. It’s just for the sake of convenience because it’s all in one file. The code demonstrates why enumerations(enum) s are still more type-safe than simple integer types, and why you should use them instead of the basic forms.

Testing the EnumerationClass
  • Close the code editor and save the source-code file.
  • Compile and execute the program on your preferred operating system using your preferred compiler.

If you followed the steps correctly, the shell window should show the following output:

Leave a Comment