Thread: enum in C++

  1. #1
    Sunset
    Guest

    Question enum in C++

    Hi,

    Can oneone explain clearly what the c++ enum data type below:

    enum {OFF = 1, ON = 2, RUN = 4, SHUTDOWN = 5};

    With no name tag, what the the code mean?I have'nt seen like
    this kind of data define before!
    Thanx!
    Sunset

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    Hi,

    If you are planning to use just constants and not create variables of the enumerated type, you can omit the an enumeration type name.

    Mr. C

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Enum makes it easier to understand your code. Instead of typing this:
    Code:
    int WalkingDirection;
    WalkingDirection = GetPressedKeyDirection();
    
    switch(WalkingDirection)
    {
      case 0:
        Xpos++;
        break;
      case 1:
        Xpos--;
        break;
      case 2:
        Ypos++;
        break;
      case 3:
        Ypos--;
        break;
    }
    You should make an enum like this:
    Code:
    typedef enum
    {
      RIGHT,
      LEFT,
      UP,
      DOWN
    }DIRECTION;
    
    
    DIRECTION WalkingDirection;
    WalkingDirection = GetPressedKeyDirection();
    
    switch(WalkingDirection)
    {
      case RIGHT:
        Xpos++;
        break;
      case LEFT:
        Xpos--;
        break;
      case DOWN:
        Ypos++;
        break;
      case UP:
        Ypos--;
        break;
    }
    It makes the code sooo much easier to read/understand.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Or like this:

    Code:
    enum DIRECTION
    {
      RIGHT,
      LEFT,
      UP,
      DOWN
    };
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM