Posts

Showing posts from June, 2020

C++ enums

C++ enumsC++ enum is a user-defined type that allows us to give textual names for integer values. Using enums you can increase code readability and reduce the probability of faults due to usage of hardcoded values. I will talk here about C++ different enumerations types and usages. Note that at least C++ 11 is required.In C++, there are two types of enumsScoped enumsUnscoped enumsUnscoped enums - called also plain enums - are very similar to the C language enums. We will start with them illustrating their C like usage, then introduce the new features added to them on C++.Unscoped enumsYou can use enums in C++ the same way as in C. The following code creates an enum that represents the traffic sign colors// Creating an enum for the traffic sign light colorsenum trafficSignColors { red, yellow, green, }; Enumerators have an integer type that can at least hold it's enumerators' greatest value. By default the first enumerator will have the value of 0 and the value will…