C++'s enum (enumeration) can have no enumeration name

Reprinted from https://blog.csdn.net/u013591613/article/details/71215000

C_enum (enumeration) can have no enumeration name

If the enumeration type is declared without specifying the enumeration name, its effect is similar to #define, such as the following code:

enum {
    STATION_IDLE = 0,
    STATION_CONNECTING,
    STATION_WRONG_PASSWORD,
    STATION_NO_AP_FOUND,
    STATION_CONNECT_FAIL,
    STATION_GOT_IP
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

It is declared here that an enumeration type does not specify its enumeration name, then it is equivalent to using #define to define six names and their corresponding values, starting from 0 and adding 1 each time, which is equivalent to:

#define     STATION_IDLE = 0;
#define     STATION_CONNECTING = 1;
#define STATION_WRONG_PASSWORD = 2; #define STATION_NO_AP_FOUND = 3; #define STATION_CONNECT_FAIL = 4; #define STATION_GOT_IP = 5;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

It's just that the eunm type represents a definite value, and here #define six values. It can be seen that if you want to represent different feedback statuses of the same event, it is better to use this enumeration without an enumeration name.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324529039&siteId=291194637