Enumeration type in c++

Definition
1. General format:
enum enumeration name {list of identifiers};

enum Weekday {
    
    SUN,MON,TUE,WED,THU,FRI,SAT};

By default, SUN=,MON=1...and so on.

2. Specify the value of the enumeration element when declaring:

enum Weekday {
    
    SUN=7,MON=1,TUE,WED,THU,FRI,SAT};

Automatic assignment of TUE and WED later

Operation
Enumeration elements cannot be assigned again, but operations and comparisons can be performed, and integer variables can also be assigned.

enum Weekday {
    
    SUN=7,MON=1,TUE=2,WED=3,THU=4,FRI=5,SAT=6};
Weekday day = SUN;
int a = day;//结果a=7

And if you want to assign a value to an enumeration type through an integer, you need to cast it.

int a = 7;
Weekday day = Weekday(a);

Note:
In the same project, if two different enumeration types have the same enumeration element, there will be conflicts. (If you want to solve this problem, you can use the enumeration class to achieve)

enum Weekday {
    
    SUN=7,MON=1,TUE=2,WED=3,THU=4,FRI=5,SAT=6};
enum Week{
    
     SUN = 7, MON = 1, TUE = 2, WED = 3, THU = 4, FRI = 5, SAT = 6 };//这一行会保错

Guess you like

Origin blog.csdn.net/qq_43530773/article/details/113729105