C++

C++ Enumerations

All, Game & Software Engineering, Programming Languages

An enumeration is a data type with a set of named variable values that is defined by the programmer. The strength behind enumerations is the ability to name a set of constant integer values for ease of readability and maintenance without adding any overhead during runtime operation.

[code language=”cpp”]
////////////////////////////////////////////////////////////////////////////////
// The enumeration defines a list of common Role Playing Game’s (RPG) classes.
// 2014/09/13 DK
enum eRPGType
{
eBard,
eBarbarian,
eFighter,
eMonk,
eRogue,
eSorcerer,
eWizard,

}; // End of enum eRPGType
[/code]

There are similar operations with enumerations compared to other data types. For example you can set a declared enum to a value and perform comparison operations with it.

[code language=”cpp”]
// Declare, define and open the program
int main()
{
// Declare an RPG type named trish and initialize it to the class type rogue
eRPGType eTrish = eRogue;

// If Trish’s class type is a Rogue then
if(eRogue == eTrish)
{
// Print to the console window
printf("Trish is a rogue! Watch your money purse.");
}
// Else if Trish is a magic user then
else if(eSorcerer >= eTrish)
{
// Print to the console window
printf("Trish is casting a fireball!");

} // End if(eRogue == eTrishClass)
// else if(eSorcerer >= eTrish)

// Return to the 0.S. a good exit
return(0);

} // End of int main()
[/code]

However, you can’t implicitly use operations to a defined enumeration; for example adding two values together in an attempt to assign the added values to an enum will cause a syntax error; or simply incrementing or decrementing an enum will also cause a syntax error. This will occur even though behind the scenes the base data type for an enumeration is an integer type. So to perform operations with enumerations you will have to perform a type cast.

[code language=”cpp”]
// Increment Trish’s RPG character class to a Sorcerer
eTrish = static_cast<eRPGType>(eTrish + 1);
[/code]

You can also perform operations with enumerations through overloading the operators in relation to the enumeration.

[code language=”cpp”]
////////////////////////////////////////////////////////////////////////////////
// The function overloads the increment operator so it can be used with
// enumeration type eRGType. 2014/09/13 DK
eRPGType &operator++(eRPGType &t)
{
// Switch on the enumeration
switch(t)
{
case eBard: return t = eBarbarian;
case eBarbarian: return t = eFighter;
case eWizard: return t = eBard;

} // End of switch(t)

} // End of eRPGType &operator++(eRPGType &t)
[/code]

By default an enumerated list like the one that we are using thus far will have its enumerated values begin with a value of zero. Each of the following enumerated values within the list will have a value of plus one to the previous value in the list; so eBard is equal to zero and eBarbarian is equal to one and so on. However, you may come across an instance where you may want to assign values to an enumerated list. You can do this by using the assignment operator within the declaration of the enumeration. The values that are not set will still have a value of plus one to the previous value.

[code language=”cpp”]
////////////////////////////////////////////////////////////////////////////////
// The enumeration defines a list of common Role Playing Game’s (RPG) classes.
// 2014/09/13 DK
enum eRPGType
{
eBard = 5, // eBard equals 5
eBarbarian, // 6
eFighter, // 7
eMonk, // 8
eRogue = 100, // 100
eSorcerer, // 101
eWizard, // 102

}; // End of enum eRPGClassType
[/code]

For better packing of your data in memory or to erase any doubts the size of your enumerated values per targeted platform then you can elect to change the base type of enumeration to something other than an integer. To do this properly use the predefined types within the standard library’s header file <cstdint>. Within the example below the enumeration is no longer the size of an interger, but that of an unsinged 8 byte integer.

[code language=”cpp”]
////////////////////////////////////////////////////////////////////////////////
// The enumeration defines a list of common Role Playing Game’s (RPG) classes.
// 2014/09/13 DK
enum eRPGType : std::uint8_t
{
eBard = 5, // eBard equals 5
eBarbarian, // 6

[/code]

With the introduction of C++ 11 came a new type of enumeration that strengthen the rules of the enumeration type. You can see the lack of strength with the traditional enumeration type (used above) when you do a compare of the enum against an integer; if(5 == eTrish) – this is bad practice, but traditional enumerations will allow you to do this. To declare a strong type enumeration use the keywords class or struct like the example below.

[code language=”cpp”]
////////////////////////////////////////////////////////////////////////////////
// The enumeration defines a list of common Role Playing Game’s (RPG) classes.
// 2014/09/13 DK
enum class eRPGType : std::uint8_t
{
eBard = 5, // eBard equals 5
eBarbarian, // 6

[/code]

With strong type enumerations you must use the scope resolution and the enum’s type name to decipher the desired enumeration within your code; if(eRGPType::eBard == eTrish). The line: if(eBard == eTrish) will now raise a syntax error if used with a strong type enum. This restriction will allow us to have enumerations with the same enumerated value names. You can see this using the example within “The C++ Programming Language” (4th Edition) by Bjarne Stroustrup:

[code language=”cpp”]
enum class Color { red, blue, green };
enum class Traffic_light { green, yellow, red };
[/code]

The use of the enumerated value red between the two declared enumerations are now valid where in our earlier example of enum eRPGType we could not use eBard in another declared enumeration. Also, by forcing our hands to write out the enum type we can now easily see what relation the enumeration has within the algorithms that it is used in.

Please, keep your comments family friendly and respectful of each other and the author.