Enum help

So i know what an enum does

1
2
3
4
5
6
7
8
9
  enum KeyPressSurfaces
{
	KEY_PRESS_SURFACE_DEFAULT,
	KEY_PRESS_SURFACE_UP,
	KEY_PRESS_SURFACE_DOWN,
	KEY_PRESS_SURFACE_LEFT,
	KEY_PRESS_SURFACE_RIGHT,
	KEY_PRESS_SURFACE_TOTAL
};

KEY_PRESS_SURFACE_DEFAULT would be set equal to 0,
KEY_PRESS_SURFACE_UP would be set equal to 1, and so on.

But, why name the enum? why not just
1
2
3
4
5
6
7
8
9
enum
{
	KEY_PRESS_SURFACE_DEFAULT,
	KEY_PRESS_SURFACE_UP,
	KEY_PRESS_SURFACE_DOWN,
	KEY_PRESS_SURFACE_LEFT,
	KEY_PRESS_SURFACE_RIGHT,
	KEY_PRESS_SURFACE_TOTAL
};


Is there ever any use of the name of the enum? or why is it there?
I looked through the code of the program and the enum name was never used or anything. I don't understand enums completely, so is there something im misinterpreting?
Is there ever any use of the name of the enum?

You can use the enum's name to make your intent clearer to other programmers.

For example, if the parameter of a function is an enum:

1
2
3
4
5
6
7
8
9
10
11
enum Colors
{
    RED,
    WHITE,
    BLUE
};

void clear_screen(Colors c)
{
    // ...
}


In the current standard of C++, named C++11 (since 2011) there are strongly typed enum's, and you can actually use the name in a way that makes sense:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
enum class Colors
{
    Red,
    White,
    Blue
};

void clear_screen(Colors c)
{
    // ...
}

int main()
{
    clear_screen(Colors::Red);
}


http://www.stroustrup.com/C++11FAQ.html#enum
Last edited on
@OP - if you have two enums:
1
2
3
4
5
6
7
8
9
10
enum FirstEnum
{
  WHITE,
  BLACK,
};
enum SecondEnum
{
  WHITE,
  BLACK,
};

You can distinguish them(FirstEnum::White, SecondEnum::White).
How would you distinguish these?
1
2
3
4
5
6
7
8
9
10
11
enum
{
  WHITE,
  BLACK,
};

enum
{
  WHITE,
  BLACK,
};

:)

And downside of enum classes is that... they are more like classes then like enums. You have to call them y EnumName::MemberName, which can get pretty annoying, also - no automatic conversions to int(which you may be used to, and sometimes you may want to use it). But on the other hand, strongly typed enums are what enums really should be.

Cheers!
For the sake of completeness, consider the following scenario.
I'm writing a card game, and I've decided to have these constant integers represent a set of discrete values - in this case, the suit of the cards.

1
2
3
4
5
6
const int CLUBS = 0, DIAMONDS = 1, HEARTS = 2, SPADES = 3;

std::string suit_name(const int suit) {
	const std::string names[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
	return names[suit];
}


The problem is, that I can pass any integer to the suit_name() function. With an enum, I can guarantee that only a suit enum can be passed, as anything else would be illegal.

1
2
3
4
5
6
enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES};

std::string suit_name(const Suit suit) {
	const std::string names[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
	return names[suit];
}
Last edited on
@MatthewRock .. i dont think what you mentioned in the example will compile though:

1
2
3
4
5
6
7
8
9
10
enum FirstEnum
{
  WHITE,
  BLACK,
};
enum SecondEnum
{
  WHITE,
  BLACK,
};


even using named enums, like in this case, i dont think it's possible to identify elements with the same name twice (i.e. WHITE or BLACK), even in the case the element is in 2 different enums (conflicting declaration).
i might be wrong though.. it doesnt compile for me anyway
By making them enum classes, the following code compiles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum class FirstEnum
{
  WHITE,
  BLACK,
};

enum class SecondEnum
{
  WHITE,
  BLACK,
};

int main()
{
}
Yes, that's what I meant. I haven't been using plain enums for some time, sorry :)
Topic archived. No new replies allowed.