Mastering C++: Part 3: Enumerators

Learning C++

Using C++

Mastering C++


Enumerators

Starting with C, there exists a way to define a fixed set of data. These are enumerators. Used through the keyword enum, this type of programming feature is often used to define certain values that will not be changed.

Enumerators work like arrays, in that their values are set to their order. In the example of the days of the week, the entry Sunday would have the value 0.

Associated Values

As a set of entries, enumerators can also have associated values. It is possible to use enumerators to hold values that stand for others.

Using Enumerators

Much like using objects in C++, an enumerator is a blueprint for entries. To use an enumerator, it must be defined in regard to a variable name.

When used as an associated value, a variable will have the value associated with the name in the enumerator. Otherwise, it will have the value of its index in the set.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.