Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The 'class name' in enum.

  1. Sep 9, 2009 #1
    An example of enum -

    enum class {a = 64, b, c, d}

    even if I -

    enum {a = 64, b, c, d}

    I will get the same results.

    Printing class returns an error...so I was wondering what's the use of this name given before the actual declaration of the elements (in braces)...or in this example, what's the use of 'class'?

    I think it makes the declarations more manageable...nothing more.

    Edit: And yes, everything in c.
     
    Last edited: Sep 9, 2009
  2. jcsd
  3. Sep 9, 2009 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    (You're talking about C, I assume?)

    enum defines a new type. You can give this type a name -- in your example, the name is class. (This example wouldn't work in C++)

    This is similar, incidentally to a struct declaration -- you need not give a structure type a name when you define it.
     
  4. Sep 9, 2009 #3
    So how can I use this custom made type?
     
  5. Sep 9, 2009 #4
    An enum is not a custom made type. It's simply a list of values a,b,c,d that all have type unsigned int.

    If you want a custom type, that's a struct (or class in C++).
     
  6. Sep 9, 2009 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    In C, enums are just another integral type -- the practical benefit of naming the type is just to make the your code more clear. e.g.

    Code (Text):

    enum game_result { win, lose, draw };

    game_result play_game(); // Definition elsewhere

    static void print_taunt(game_result result)
    {
      if(result == win) { printf("Haha, I won!\n"); }
      if(result == lose) { printf("You got lucky.\n"); }
      if(result == draw) { printf("Nyah nyah!\n"); }
    }

    int main(int, char*[])
    {
      game_result result = play_game();
      print_taunt(result);
    }
     
  7. Sep 9, 2009 #6
    So game_results has no significance here...
     
  8. Sep 9, 2009 #7

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Yes it does, game_result can only be assigned/compared to another game result - so using it the way hurkly showed stops you assigning any value to game_result that isn't in the list.

    With no mane in the enum you can assign/compare the values with any int.
     
  9. Sep 9, 2009 #8
    cixelsyd much?
     
  10. Sep 9, 2009 #9

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Laptop on couch, broken keyboard (D,Y need hitting hard) and I can't touch type.
     
  11. Sep 20, 2009 #10
    Game results has 3 constants -
    win, lose, draw with values 1, 2 and 3 respectively.

    So what does this statement -

    Code (Text):
    game_result play_game(); // Definition elsewhere
    Do apart from calling the function play_game?

    And what does this function take as input -

    static void print_taunt(game_result result)

    BTW this is the call -

    print_taunt(result);

    So what's this game_result doing in the print_taunt function?

    Overall...I don't get a thing. :cry:
     
  12. Sep 21, 2009 #11

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    It's been too long since you programmed in C, Hurkyl. That won't compile. It would compile if the first line was
    Code (Text):
    typedef enum { win, lose, draw } game_result;

    That does not call the function play_game. This calls the function play_game:
    Code (Text):
     result = play_game();
    For the compiler to make sense of this line, you need to told the compiler earlier in your code that play_game is a function that returns something of type game_result. This line does just that:
    Code (Text):
    game_result play_game(); // Definition elsewhere
    To make sense of this function declaration statement, even earlier in your code you need to have told the compiler that game_result is a new type. Something like
    Code (Text):
    typedef enum { win, lose, draw } game_result;
    It obviously takes a variable of type game_result as input.
     
  13. Sep 22, 2009 #12
    I guess I'll do it later on...thanks everyone!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: The 'class name' in enum.
  1. Ambiguous name (Replies: 5)

  2. Class conundrum (Replies: 4)

  3. Classes and objects (Replies: 6)

  4. Pointers and classes (Replies: 1)

Loading...