enum in C++

This is a discussion on enum in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi, Can oneone explain clearly what the c++ enum data type below: enum {OFF = 1, ON = 2, RUN ...

  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, 10: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, 04:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 09:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21