Some good example for using enums

后端 未结 10 1522
感情败类
感情败类 2021-01-05 06:28

I learned enums when I learned C and from time to time I keep myself reminding about it and most of the time by re-reading from some source,it occurred to me that this is d

相关标签:
10条回答
  • 2021-01-05 07:19

    You can have first value and last value and everything else inbetween those values. The every where in the code check if your values are in the range. NOW add new values to your enum between first and last and don't wory abouth changing all of these checks!

    typedef enum
    {
       First_value, 
       Uninitialized,
       Initialization,
       Active,
       Idle,
       Last_value
    } my_type;
    
    void function(my_type state)
    {
       if ((state > First_value) && (state < Last_value))
       {
           //Do stuff...
       }
    }
    
    0 讨论(0)
  • 2021-01-05 07:19

    I started a personal project in, and I wanted to identify my packet ID, it looks like this:

    enum{
    //Client to server
        //Connection
        ID_KEEP_ALIVE       = 0x00,
        ID_LOGIN_REQUEST    = 0x01,
        ID_CONNECTING       = 0x02,
        ID_DISCONNECT       = 0x03,
    
        //Player actions
        ID_PLAYER_INFO      = 0x04,
        ID_PLAYER_MOVE      = 0x05,
        ID_PLAYER_ATTACK    = 0x06,
    
        //Inventory
        ID_LOOT_ITEM        = 0x10,
        ID_DESTROY_ITEM     = 0x12,
        ID_USE_ITEM         = 0x13,
        ID_EQUIP_ITEM       = 0x15,
        ID_UNEQUIP_ITEM     = 0x16,
        ID_DROP_ITEM        = 0x17,
    };
    

    and then, when I receive a packet, I've got a huge switch that looks like this to process the packets and send them:

    switch(packet.packetID){
        case ID_KEEP_ALIVE:
            //...
            break;
        case ID_LOGIN_REQUEST:
            //...
            break;
        case ID_CONNECTING:
            //...
            break;
        case ID_DISCONNECT:
            //...
            break;
        //..
    }
    

    it's my best example, enjoy :)

    0 讨论(0)
  • 2021-01-05 07:21

    Imagine that you are programming a depth first search and you want to tag your edges with whether they are tree, back, forward, or cross. You could create an enum EDGE_TYPE with the four possibilities and use it to tag your edges.

    0 讨论(0)
  • 2021-01-05 07:22

    When describing/observing some attribute of some system you might find that attribute can have any value from a limited set. Name those values, assign each an integer value (code), collect them in an enumeration and you have defined a type of that attribute. All actions considering that attribute can now use this type.

    Example: for some system we can consider its state as one of its attributes. We can observe it and say that it can be in 'uninitialized' state, state of 'initialization', 'active' or 'idle' state (feel free to add more states here...). If you want to perform some action on that system but which depends on the current state, how will you pass state information to that action (function)? You can pass strings 'uninitialized', 'initialization'...but more efficient, simple and safe from errors would be if you would pass just one integer from a set:

    enum State
    {
       Uninitialized,
       Initialization,
       Active,
       Idle
    };
    

    That function will have State as an argument and can use switch when deciding what to do depending on the current state:

    void foo(..., const State state,...)
    {
       ...
       switch(state)
       {
          case Uninitialized:
              cout << "Uninitialized" << endl;
              break;
          case Initialization:
              ...
       }
       ...
    }
    

    Using enumeration type to describe a limited set of attribute's values is safer then using a set of #defines and integer variable. E.g. if you have:

    #define UNINITIALIZED  0
    #define INITIALIZATION 1
    #define ACTIVE         2
    #define IDLE           3
    

    and

    int nState;
    

    nothing can stop you to assign any integer value to nState:

    nState = 4; // What state is 4?
    

    If you use enumeration:

    State state;
    

    You cannot assign to it an arbitrary integer value but only an enumerator (although underlying type for enumeration is integer! - see this):

    state = Active;
    
    0 讨论(0)
提交回复
热议问题