C/C++ enum enumeration

C/C++ enum enumerator

  • foreword
    • Step (1) – declaration and definition of enumeration
    • Step (2) – Customize the value of the enumeration
    • Step (3) – the value range of the enumeration
    • Step (4) – enumerate applications
  • strongly typed enum
    • grammar
    • Strongly typed enum code snippet in project
      • 1. Image processing
      • 2. Traffic lights
      • 3. Application of forward declaration

Foreword

As we all know, the C/C++ language can use #define and const to create symbolic constants, and the enum tool can not only create symbolic constants, but also define new data types, but it must be done according to certain rules. Let’s take a look at enum together usage method.

Step (1)–Declaration and definition of enumeration

(1) First, look at the following statement:

enum enumType {<!-- -->Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

This sentence has two effects:
First: declare enumType as a new data type, called enumeration (enumeration);
Second: Declare Monday, Tuesday, etc. as symbolic constants, usually called enumerators, whose values are 0-6 by default. (I will introduce how to explicitly initialize the value of the enumerator later)
(2) Then use the new enumeration type enumType to declare a variable of this type:
enumType Weekday is just like using the basic variable type int to declare variables, such as int a; You can also define enumeration variables when defining enumeration types

enum enumType {<!-- -->Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}Weekday;

However, the difference from the basic variable type is that, on the premise of no mandatory conversion, only the defined enumeration value can be assigned to the enumeration variable of this type, such as: Weekday = Monday; or Weekday = Sunday; cannot assign other values to enumeration variables, such as: Weekday = 10; This is not allowed, because 10 is not an enumeration value. That is to say, Weekday can only be the defined enumeration of Monday-Sunday. However, this is not absolute. Article 6 will talk about using mandatory type conversion to assign other types of values to enumeration variables.

(3) As mentioned above, non-enumeration variables cannot be assigned to enumeration variables, so can enumeration variables be assigned to non-enumeration variables?

Such as: int a=Monday; This is allowed, because the enumerator is a symbolic constant, the assignment compiler here will automatically convert the enumerator to int type.

(4) As mentioned earlier, enumerations can be assigned values, so can enumeration variables perform arithmetic operations?

Weekday + + ;Weekday = Monday + Tuesday; This is illegal because these operations may result in violation of type restrictions, such as: Weekday = Sunday;

Weekday + + ;Weekday is first assigned the last value Sunday (value 6) in the enumeration, and if it is incremented, Weekday increases to 7, and for enumType type, 7 is invalid.

Summary: For enumerations, only assignment operators are defined, and no arithmetic operations are defined for enumerations.

(5) Arithmetic operations cannot be performed on enumerations, so can enumerations participate in operations on other types of variables?

int a;a = 1 + Monday;This is allowed, because the compiler will automatically convert the enumerator to int type.

(6) The second article: On the premise of no mandatory conversion, only the defined enumeration can be assigned to the variable of this type of enumeration. The implication is that other types of values can be assigned to the enumeration through mandatory conversion. Variables:

Weekday = enumType(2); is equivalent to: Weekday = Wednesday;However, if you try to assign a value beyond the enumeration range to the enumeration variable by coercion , what happens?

Weekday = enumType(20);The result will be indeterminate, so doing this will not go wrong, but you won’t get the desired result.

Step (2) – Customize the value of the enumeration

(1) As mentioned above, by defining enum enumType {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

The values of the enumerations Monday, Tuesday, etc. are 0-6 by default, and we can explicitly set the value of the enumeration:

enum enumType {Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6, Sunday=7};

The specified value must be an integer!

(2) It is also possible to explicitly define only a part of the enumeration value:

enum enumType {Monday=1, Tuesday, Wednesday=1, Thursday, Friday, Saturday, Sunday}; In this way, Monday and Wednesday are both defined as 1, then Tuesday=2, Thursday, Friday, Saturday and Sunday are 2 by default , 3, 4, 5. Summary: The value of an uninitialized enumeration value will be 1 greater than its previous enumeration value by default.

(3) The second article also explains another phenomenon, that is, the values of enumerations can be the same.

Step (3) – the value range of the enumeration

As mentioned earlier, other types of values can be assigned to enumeration variables through coercion: Weekday = enumType(2); This is legal; but Weekday = enumType(20); is illegal.

This involves the concept of the range of enumeration values: The upper limit of the enumeration is the smallest power of 2 that is greater than the maximum enumeration amount, minus 1;

There are two cases for the lower bound of an enumeration:

1. If the minimum value of the enumeration quantity is not less than 0, then the enumeration lower limit is 0;

2. The minimum enumeration value is less than 0, then the enumeration lower limit is the largest power of 2 that is less than the minimum enumeration value, plus 1.

For example:

If you define enum enumType1 { First=-5, Second=14, Third=10 }; then the upper limit of the enumeration is 16-1=15 (16 is greater than the maximum enumeration amount of 14, and is a power of 2); the lower limit of the enumeration It is -8 + 1=-7 (-8 is less than the minimum enumeration -5, and is a power of 2);

Step (4) – enumerate applications

enum enumType{<!-- -->
Step0, Step1, Step2
}Step=Step0;
//Note that the enumeration variable Step is directly defined when declaring the enumeration, and initialized to Step0

```c
switch (Step)
{<!-- -->

  case Step0:{<!-- -->...;break;}

  case Step1:{<!-- -->...;break;}

  case Step2:{<!-- -->...;break;}

  default: break;

}

Another rare usage of enumeration is enum { one , two , three}; just does not specify a name, so we naturally cannot define some enumeration types. At this time, it is equivalent to static const int one = 0; this is the same as defining three constants. Then use int no = one.

Strongly typed enumeration

Strongly-typed enumeration (Strongly-typed enums), known as enumeration type, is a new syntax in C++11 to solve the defects of traditional C++ enumeration types. In traditional C++, enumeration constants are exposed in the outer scope, so if there are two different enumeration types in the same scope, but the same enumeration constants are not allowed, for example:

enum Side{<!-- -->Right,Left};

enum Thing{<!-- -->Wrong,Right};

This cannot be used together.

Another flaw is that traditional enumeration values are always implicitly converted to integers, and users cannot define their own types. Strongly typed enums in C++11 solve these problems.

Syntax

Strongly typed enumerations are declared using the enum class syntax, as follows:

enum class Enumeration{<!-- -->

VAL1,

VAL2,

VAL3=100,

VAL4

};

In this way, the enumeration type is safe, and the enumeration value will not be implicitly converted to an integer, and cannot be compared with an integer value, for example (Enumeration::VAL4==10 will trigger a compilation error).

In addition, the type used by the enumeration type defaults to the int type, and other types can also be specified, such as:

enum cass Enum:unsigned int{<!-- -->VAL1,VAL2};

As mentioned above, strongly typed enumeration can solve the problem of the same enumeration value name under different enumeration classes in traditional enumeration. When using the enumeration name of enumeration type, you must indicate the scope to which it belongs, for example: Enum:: VAL1, and VAL1 alone is no longer meaningful.

It is also worth noting that the forward declaration of the enumeration type in C++11 is also feasible, such as:

enum class Enum;

enum class Enum1: unsigned int;

Strongly typed enum code snippet in the project

1. Image processing

enum class Color{<!-- -->RED,BLUE,YELLOR,BLACK,WHITE};

2. Traffic lights

enum class TrafficLight{<!-- -->RED,YELLOR,GREEN};

Strongly typed enumeration values have the functions of traditional enumerations – named enumeration values, and at the same time have the characteristics of classes – have members of class fields and cannot perform default type conversion. So it is also called an enumeration class – enmu class

The underlying data of the enumeration class must be signed or unsigned integer, such as char unsigned int unsigned long, the default is int.

3. Forward declaration application

enum class Color:char; //pre-declaration

void Foo(Color *p); //Use of forward declaration

enum class Color:char{<!-- -->RED,GREEN,BLACK,WHITE}; //Definition