Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
enum is a keyword to define an
enumeration. Use enumerations to represent sets of named constants [7].
Define operations on enumerations for safe and simple use [9].
Avoid enumerations at file scope in header files [5].
Use a consistent method to identify immutable values such as enum values [6].
In C++11, prefer class enums over plain enums to minimize surprise [8].

Example: with and without enum
Below is an example of a code that did not use enum.
How can one expect to memorize all these values for sexes? enum relieves things:
Of course, the example without enum can be converted to the example below,
using global constants. Prefer not to use globals [1-4].
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 10: 'Minimize global and shared data'
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 1.8.2.a: 'Don't use global data (use members)'
- Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. 2002. ISBN:0-672-32480-6. Chapter 3: 'Avoid using global variables'
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 2: Prefer consts, enums and inlines to #defines
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.3.3: 'Avoid enumerations, typedefs and constants at file scope in .h files'
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.7: 'Use a consistent method (such as all uppercase with underscores) to identify immutable values such as enumerators, const data and preprocessor constants'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[5] Use enumerations to represent sets of named constants'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[6] Prefer class enums over "plain" enums to minimize surprises'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[7] Define operations on enumerations for safe and simple use'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[6] Prefer class enums over "plain" enums to minimize surprises'
- Scott Meyers. C++ And Beyond 2012 session: 'Initial thoughts on Effective C++11'. 2012. ' Prefer enum classes to enums'
- Trevor Misfeldt, Gregory Bumgardner, Andrew Gray. The elements of C++ style. 2004. ISBN: 978-0-521-89308-4. Chapter 4.2, page 18: 'Use UpperCamelCase for classes, constants, structures, enumerations, and typedefs'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
