A good example of using enums

I learned enums when I learned C, and from time to time I constantly remind about it and most of the time, reading from some source, it occurred to me that this is due to the fact that I never use it in my programming, my interest in programming is focused on solving algorithmic problems, so I'm not sure where I can use enumerations.

Can anyone suggest a good example when using enumerations makes things very easy?

I would appreciate algorithmic examples, but both algorithmic or nonalgorithmic examples are welcome.

+5
source share
10 answers

, , , , , . enum EDGE_TYPE .

+18

/ - - , . , (), , . , , .

: . , "" , "", "" "" ( ...). - , , ()? "", ""... , , :

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

, :

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

, #defines integer variable. . :

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

int nState;

nState:

nState = 4; // What state is 4?

:

State state;

, ( ! - . this):

state = Active;
+11

, , .

+7

enum .

: -

+4

- . :

//Usage: Kick(Dog);

enum PetType
{
   Cat,
   Dog
};

void Kick(PetType p)
{
   switch(p)
   {
      case Cat:
        //Kick Cat
        break;
      case Dog:
        //Kick Dog
        break;
      default:
        //Throw an exception.
        break;
    }
}

//Usage: Kick(false);

void Kick(bool isCat)
{
    if (isCat)
    {
        //Kick Cat
    }
    else
    {
        //Kick Dog
    }
}

, , -, , , . Kick(Dog) , Kick(false).

+4

, , ++ enum , , ,

class BlockCipher {
public:
  enum PaddingOptions { NO_PADDING, DEFAULT_PADDING, ZERO_PADDING /*...*/ }
  void encrypt(const std::string& cleartext, std::string& ciphertext, 
               PaddingOptions pad=DEFAULT_PADDING);
};

int main()
{
  std::string clear("hello, world");
  std::string encrypted;
  BlockCipher  encryptor;

  encryptor.encrypt(clear, encrypted, BlockCipher::NO_PADDING);
}
+4

enum .

  • int unsigned int , .
  • . enum std::cout enum , . enum .

enum . , . , , . , .

:

  • switch.
  • .
  • .
  • .
+2

. , , , . !

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...
   }
}
+1

#define, : / enum, #define d.

, #define , , #ifdef. , , , .

, enum, :

#define FOO FOO
#define BAR BAR

.

0

, , :

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,
};

, , , , :

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

, :)

0

All Articles