Variable declaration is important in programming and there is an enumeration method that allows you to numerate/count variables easily. In C++, enumeration is very important and widely used in different syntaxes. Enumeration in C++ can be done with the enum
keyword which can be used to create unscoped and scoped enumerations. C++11 and above has modern enumeration methods that can be easily used in a modern C++ Code Editor and compiler. In this post, we explain what strongly typed enums in C++ are and how to use them.
First of all, let’s learn what enumeration in C++ means, and which types of enumerations we can use.
Table of Contents
What is enumeration and how to use enum C++?
An enumeration is used to declare constants in a range of values easily. It may include several explicitly named constants which are also called as enumerators. In C++, enumeration can be done with enum
keyword which is also known as enum-key. enum can be used in two ways in C++. These are,
- Unscoped enumeration
- Scoped enumeration (strongly typed enums)
In this article we explain how to use scoped enumeration in C++, in other words strongly typed enums.
Learn how to use strongly typed enums in Modern C++
Scoped enumeration types were introduced with the C++11 standards. In this enumeration type, the enum keyword is used with a class or struct to declare a scoped enumeration type. Its fundamental type can be fixed, not fixed or can be used to specify the name and the type.
The scoped enumerations can be used in 3 different types of syntax, these are,
enum
syntax for the scoped enumeration type whose type is not fixed:
1 |
enum <struct|class name> { enumerator = constexpr , enumerator = constexpr , ... } |
here is an example,
1 2 |
enum class material { wood, metal, glass } ; // wood = 0, metal = 1, glass = 2 material t = material::wood; //using material:: scope |
2. enum
syntax for the scoped enumeration type whose type is fixed,
1 |
enum <struct|class name> : type { enumerator = constexpr , enumerator = constexpr , ... } |
here is an example,
1 2 |
enum class difficulty:unsigned char { easy= 10, modarate = 20, hard = 30 } ; difficulty cpp = difficulty::easy; |
3. enum
syntax for the scoped enumeration type which is used to specify the name and the type.,
1 |
enum <struct|class name> : type ; |
here is an example,
1 |
enum class mode:unsigned char; |
Is there a simple example of how to use strongly typed enums in Modern C++?
Here is a simple example of strongly typed enums in Modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> int main() { enum class material { wood, metal, glass }; // wood = 0, metal = 1, glass = 2 material t = material::wood; // using scope material:: std::cout << t ; system("pause"); return 0; } |
Is there a full example of how to declare and use strongly typed enums in Modern C++?
Here is a full example of strongly typed enums in Modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> int main() { // scoped enumeration type whose type is not fixed enum class material { wood, metal, glass }; material t = material::wood; switch(t) { case material::wood: std::cout << "wood material" << std::endl; break; case material::metal: std::cout << "metal material" << std::endl; break; case material::glass: std::cout << "glass material" << std::endl; break; } // scoped enumeration type whose type is fixed (here unsigned char), enum class difficulty:unsigned char { easy= 10, modarate = 20, hard = 30 } ; difficulty cpp = difficulty::easy; // scoped enumeration type which is used to specify the name enum class type; // scoped enumeration type which is used to specify the name and the type. enum class mode:unsigned char; system("pause"); return 0; } |
These are the simple scoped enumeration examples. In the unscoped enumeration, we don’t use struct or class as a scope for that enumeration. This is explained in another article in LearnCPlusPlus.org.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.