PDA

View Full Version : The 'class name' in enum.


dE_logics
Sep9-09, 12:40 AM
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.

Hurkyl
Sep9-09, 12:53 AM
(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.

dE_logics
Sep9-09, 12:58 AM
So how can I use this custom made type?

junglebeast
Sep9-09, 01:11 AM
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++).

Hurkyl
Sep9-09, 01:17 AM
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.


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

dE_logics
Sep9-09, 06:14 AM
So game_results has no significance here...

mgb_phys
Sep9-09, 08:13 AM
So game_results has no significance here...

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.

junglebeast
Sep9-09, 10:46 PM
With no mane in the enum you can assign/compare the values with any int.

cixelsyd much?

mgb_phys
Sep9-09, 11:08 PM
Laptop on couch, broken keyboard (D,Y need hitting hard) and I can't touch type.

dE_logics
Sep20-09, 11:09 PM
Game results has 3 constants -
win, lose, draw with values 1, 2 and 3 respectively.

So what does this statement -

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:

D H
Sep21-09, 05:09 AM
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.


enum game_result { win, lose, draw };

game_result play_game(); // Definition elsewhere
static void print_taunt(game_result result)
...

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



So what does this statement - game_result play_game(); // Definition elsewhere
Do apart from calling the function play_game?
That does not call the function play_game. This calls the function play_game:
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: 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 liketypedef enum { win, lose, draw } game_result;

And what does this function take as input -

static void print_taunt(game_result result)
It obviously takes a variable of type game_result as input.

dE_logics
Sep22-09, 01:44 AM
I guess I'll do it later on...thanks everyone!