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

  • Thread starter Thread starter dE_logics
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the purpose and implications of using the 'class' keyword before enum declarations in C, with a focus on type definition, clarity in code, and the differences between enums and other types like structs. Participants explore the utility of naming enums and the significance of type safety in programming.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants note that enums define a new type, and naming the type (e.g., 'class') can enhance code clarity.
  • Others argue that enums in C are simply lists of integral values and do not constitute custom types like structs or classes in C++.
  • A participant questions the significance of naming an enum, suggesting that it may not add functional value.
  • Another participant counters that naming an enum type restricts assignments and comparisons to only the defined values, enhancing type safety.
  • There is confusion about the function declaration and its purpose, with some participants clarifying that it does not call the function but rather declares its return type.
  • Participants discuss the implications of using typedef with enums to create more meaningful type definitions.

Areas of Agreement / Disagreement

Participants express differing views on the significance of naming enums and whether it provides any real benefits. The discussion remains unresolved regarding the necessity and implications of using 'class' in enum declarations.

Contextual Notes

Some participants highlight that the discussion is specific to C and may not apply to C++ or other programming languages, indicating potential limitations in the applicability of the arguments presented.

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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K