Enums

Learn about Enums and how they offer an efficient way to handle predefined values in your code

Ryan McCombe
Updated

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"

Defining Enums

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
};

Test your Knowledge

Creating Enums

How can we create an enum called Mood?

Using Enums

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() };

Test your Knowledge

Using Enums

How can we use our Mood enum to add a variable to the Character class?

enum class Mood { Friendly, Neutral, Hostile };

class Character {
  // ?
};

Advantages of Enums

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.

Enums combine the performance of integers with the readability of strings. Additionally, they have even more advantages:

  • The compiler will only permit valid values - there's no risk we introduce a bug by mistyping a string, or using a number that doesn't correspond to any value.
  • Because an 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.

Summary

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:

  • Enums allow for defining a variable to be one of a limited set of predefined values, like Faction with options such as Goblin, Undead, and Troll.
  • The enum class syntax provides a strongly typed enumeration.
  • Enums are more efficient and readable than using integers or strings for multiple-choice variables.
  • The scope resolution operator :: is used to access enum values.
  • Enums, being stored as integers internally, are fast to compare and help prevent errors compared to strings or plain integers.
Next Lesson
Lesson 43 of 60

The using Keyword

This lesson introduces the using keyword in C++, focusing on namespaces, enums, and type aliasing

Have a question about this lesson?
Answers are generated by AI models and may not have been reviewed for accuracy