Use an enum or enum Class?
Depending on the nature of the Enum and enum class, we can decide whether to use an enum or an Enum class based on the requirements of the constant type .
The following scenarios are appropriate for using enum:
- Constant types are used for internal representations and are not used to display names.
- Constant values do not need to provide additional properties. For example, you only need to know the country code, without having to get other properties of the country
- Enumeration values allow combinations (that is, support bit operations).
The Enum class can be used for more scenarios:
- Often used for types that provide friendly information. such as a localized supported type name, or a name inconsistent with the enumeration name, such as Country.chn, can be displayed as "China".
- Provides more constant properties.
- To provide richer behavior. such as the Parse method.
- Groups the constants. such as Country.asia contains Asian countries.
Using structs to represent enumerations
if the range is not closed, but you want to provide some constants, you can also use a struct, such as the system default color setting in the System.Drawing.Color structure. There is no essential difference between using struct to design enum values and enum class, except that struct provides parameterless constructors by default and therefore cannot implement a closed domain. \
\
Anyone who wants to be proficient in C + + should actually read the standard documents of C + +, rather than just reading the books written by others, first-hand data, especially standard documents, are always irreplaceable. Then there are other books that help to understand these standards more deeply.
The reason for providing this enum class is because the old enum has a number of drawbacks. Briefly describe:
1. Easy to be implicitly converted to int
2. Underlying type refers to the implementation details behind the compiler implementation that lead to cross-platform, cross-compiler inconsistencies. Dimensions are not estimated and so on.
3. No strict scope limits
The document also analyzes the methods to try to solve the above problems, but ultimately because of their own shortcomings, or create an enum class to solve. Technology is always evolving, and the Enum class today solves the problems of the past and may soon find its drawbacks. :)
Below I have written an example:
#ifndef Bean_rest_code_h_#define Bean_rest_code_h_enum class Restcode {OK, add_cpu_error}; #endif
A sigh of relief, fortunately not as complex as the Java enum.
Now it can't be implicitly converted to int, so what if I really need to do this? such as serialization into the flow inside. With Underlying_type. From StackOverflow a post, it is said that this code comes from an open source library.
#ifndef helper_enum_helper_h_#define helper_enum_helper_h_template <typename enumeration>auto As_integer ( Enumeration const value), TypeName Std::underlying_type<enumeration>::type{return Static_cast<typename Std::underlying_type<enumeration>::type> (value);} #endif
As for how the syntax is used, the documentation is described in detail. Read it carefully.
enum, enum Class?