What is the purpose of adding 'class' before enum declarations?

  • Thread starter Thread starter dE_logics
  • Start date Start date
AI Thread Summary
The discussion centers on the use of enums in C programming, specifically the distinction between defining an enum with a name versus without one. It highlights that naming an enum creates a new type, which enhances code clarity and restricts assignments to valid enum values, unlike unnamed enums that can be assigned any integer. The example provided illustrates how naming an enum, such as `game_result`, allows for type-safe function parameters and return types, preventing invalid assignments. The conversation also clarifies misconceptions about function declarations and the purpose of typedefs in defining custom types. Overall, the key takeaway is that while enums in C are integral types, naming them provides significant benefits in terms of code readability and type safety.
dE_logics
Messages
742
Reaction score
0
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:
Technology news on Phys.org
(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.
 
So how can I use this custom made type?
 
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++).
 
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:
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);
}
 
So game_results has no significance here...
 
dE_logics said:
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.
 
mgb_phys said:
With no mane in the enum you can assign/compare the values with any int.

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

So what does this statement -

Code:
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:
 
  • #11
Hurkyl said:
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:
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
Code:
typedef enum { win, lose, draw } game_result;
dE_logics said:
So what does this statement -
Code:
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:
Code:
 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:
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:
typedef 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.
 
  • #12
I guess I'll do it later on...thanks everyone!
 
Back
Top