C++ Enums

Learn how we can use enumerated classes to deal with scenarios where a value can be one of several possibilities.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a fantasy character
Ryan McCombe
Ryan McCombe
Posted

Another common data requirement we will soon run in to 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 a number of possibilities?

We could imagine a need to store a character's faction, for example, with possible values being "Human", "Elf", and "Undead"

This is where the enum comes in:

enum class Faction {
   Human,
   Elf,
   Undead
}

Test your Knowledge

How can we create an enum called Hostility?

Variables that store one of these values will have a type of Faction, and we can access the options using the scope resolution operator ::. This is shown in the below examples:

Faction PlayerFaction { Faction::Human };
bool isElf(Faction SelectedFaction) {
  return SelectedFaction == Faction::Elf;
}
class Vampire {
public:
  Faction GetFaction() { return Faction };
private:
  Faction Faction { Faction::Undead }
}

Vampire Enemy;
Faction EnemyFaction { Enemy.GetFaction() };

Test your Knowledge

How can use our Hostility enum to add a variable to this class on line 4?

1enum class Hostility { Friendly, Neutral, Hostile };
2
3class Character {
4  // ?
5};
6

using Statements with Enums

In some files, we may want to use the same enum over and over again, and the constant qualification of MyEnumName:: would make our code excessively verbose.

We can solve this problem in the same we we tacked the equivalent issue with namespaces - by adding a using statement.

This can take our code from something like this:

enum class Faction { Human, Elf, Undead };

Faction MyFaction { Faction::Human };

To this:

enum class Faction { Human, Elf, Undead };

using enum Faction;

Faction MyFaction { Human };

Note, using enum is a recent addition to the language, included as part of the C++ 20 spec that was published in 2021. It may not yet be supported by all compilers.

Test your Knowledge

What statement can we add on line 3 above that will make line 6 work?

1enum class Hostility { Friendly, Neutral, Hostile };
2
3// ?
4
5class Character {
6  Hostility Hostility { Friendly };
7};
8

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 an elf or a dwarf?

Strings like "dwarf" solve the readability problem. However, they consume more memory and network resources than an int, and they're also slower to compare.

Comparing Strings

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 every character needs compared, one by one. A request like "dwarf" == "dwarf" would require at least 5 operations - one for each character

Enums are fast to compare because, behind the scenes, they're just stored as integers by default.

The enum syntax is some compiler magic 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:

  • 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.

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Namespaces, Enums and Structs
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

C++ Structs vs Classes

An introduction to structs - an alternative to classes that provides another way to define our own data types.
3D art showing a female character
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved