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

  • Thread starter dE_logics
  • Start date
In summary,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++).
  • #1
dE_logics
742
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
  • #2
(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.
 
  • #3
So how can I use this custom made type?
 
  • #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++).
 
  • #5
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);
}
 
  • #6
So game_results has no significance here...
 
  • #7
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.
 
  • #8
mgb_phys said:
With no mane in the enum you can assign/compare the values with any int.

cixelsyd much?
 
  • #9
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!
 

1. What is an enum?

An enum, short for enumeration, is a data type in programming that consists of a set of named values. These values are typically constants that represent a specific category or type of data.

2. How is an enum declared?

An enum is declared by using the keyword "enum" followed by the name of the enum and a set of curly braces containing the enum constants. For example: enum Colors {RED, GREEN, BLUE}.

3. What is the purpose of using an enum?

The purpose of using an enum is to make the code more organized, readable, and maintainable. Enums provide a way to define a set of related constants and use them throughout the code instead of using arbitrary values. This can also prevent coding errors and improve code efficiency.

4. Can an enum have methods?

Yes, an enum can have methods. These methods can be used to perform operations on the enum constants, such as getting their values or comparing them to other enums. Enum methods can also be used to add additional functionality to the enum.

5. How do I access the values of an enum?

To access the values of an enum, you can use the dot notation. For example, if you have an enum named "Days" with constants "MONDAY", "TUESDAY", and "WEDNESDAY", you can access them by using Days.MONDAY, Days.TUESDAY, and Days.WEDNESDAY.

Similar threads

  • Programming and Computer Science
Replies
2
Views
351
  • Programming and Computer Science
Replies
8
Views
847
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
6
Views
898
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top