Enumerations

The C++ enum facility provides an alternative means to const for creating symbolic constants. It also lets you define new types but in a fairly restricted fashion. The syntax for using enum resembles structure syntax. For example, consider the following statement:

enum spectrum {red, orange, yellow, green, blue, violet, indigo, ultraviolet};

This statement does two things:

  • It makes spectrum the name of a new type; spectrum is termed an enumeration, much as a struct variable is called a structure.

  • It establishes red, orange, yellow, and so on, as symbolic constants for the integer values 0—7. These constants are called enumerators.

By default, enumerators are assigned integer values starting with 0 for the first enumerator, 1 for ...

Get C++ Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.