Another common data requirement we will soon run into is a desire to have a property be one of a limited set of values.
We've seen booleans
for capturing one of two states - true
or false
, but what if we want a property to be one of three, four, or more possibilities?
We could imagine a need to store a character's faction, for example, with possible values being "Goblin", "Undead", and "Troll"
This is where the concept of an enumerated type is useful. These are sometimes called enumerations, or simply enums. We create an enum
in C++ like this:
enum class Faction {
Goblin,
Undead,
Troll,
Dragon
};
How can we create an enum called Mood
?
Variables that store one of these values will have a type of Faction
, and we can access the options using the scope resolution operator ::
. Three examples of this are shown below:
Faction EnemyType { Faction::Dragon };
bool isTroll(Faction SelectedFaction) {
return SelectedFaction == Faction::Troll;
}
class Vampire {
public:
Faction GetFaction() { return mFaction; }
private:
Faction mFaction { Faction::Undead }
};
Vampire Enemy;
Faction EnemyFaction { Enemy.GetFaction() };
How can use our Mood
enum to add a variable to the Character
class?
enum class Mood { Friendly, Neutral, Hostile };
class Character {
// ?
};
We could have captured these multiple-choice variables as some other type - for example, integers or strings.
But, if we use integers, our code becomes quite difficult to follow, as we have to remember what the numbers mean. For example, is 3
a troll or a dragon?
Strings like "Dragon"
solve the readability problem. However, they consume more memory and network resources than an int
, and they're also slower to compare.
When our software asks a computer to compare two integers, for example, 3 == 3
, that can be done in a single operation.
Comparing strings is a bit more involved, as potentially every character needs to be compared, one by one. A request like "Dwarf" == "Dwarf"
would typically require 6 checks - one for each character, and an additional check to ensure there are no further characters.
Enums are fast to compare because, behind the scenes, they're just stored as integers by default.
The enum syntax is simply some compiler syntax to let us solve this problem by attaching some semantic meaning to a collection of integers.
Enums combine the performance of integers with the readability of strings. Additionally, they have even more advantages:
enum
defines what the possible options are, our tools can be helpful. For example, our code editor will generally provide auto-complete support when we're working with enums. As soon as we type Faction::
, our IDE will likely display the range of Faction
values available to us.This lesson introduced the concept of Enums in C++. Enums allow us to represent a set of predefined values, enhancing both code readability and efficiency.
Key Learnings:
Faction
with options such as Goblin, Undead, and Troll.enum class
syntax provides a strongly typed enumeration.::
is used to access enum values.In our upcoming lesson, we'll dive into the use of using
statements, allowing us to simplify our code and enhance readability.
Key Topics to Be Covered:
using
statement and its role in simplifying code.using
statements in namespaces to avoid namespace pollution.using
statements with enums to streamline code readability and usage.using
statements for type aliases.using
statements in various C++ programming scenarios.Learn about Enums and how they offer an efficient way to handle predefined values in your code
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way