Benefits of enum

Pages: 12
Hi,

I've got a question about using enum in classes.
Let's take a look at a sample car class:
1
2
3
4
5
6
7
  class car
  {
    public:
     enum color {BLUE, RED, GREEN, GREY, BLACK, WHITE);

     car();
  };

Where is the benefit while using enum color to define the cars color instead of simply using a string color for example?
1
2
3
4
5
6
7
class car
{
  public:
    string color;

    car();
  };


I don't really realize why and when I should use enums to define something..
Strings can have almost any value in them, while enums can only have what is defined. If you had a string, you could accidentally write car.color = "WHOTE" instead of white, but with an enum that would give you an error.
Hi,
thank you for the fast answer!
Now I have one more question:
When I declare an enum type in my class:
1
2
3
4
5
6
class car
{
enum color {BLACK, WHITE, BLUE, RED};
color c;
int a, b;
};

How do I use it? What I have now is a "struct" c with 4 members (BLACK, WHITE, BLUE, RED).. but how can I select one now and how do I check which member of "c" is selected at the moment?
I mean, doing something like:
c = color::WHITE; wouldn't make any sense for me, because WHITE is not a value but a variable...
Last edited on
Also enums are simple integral values internally. This makes it efficient to pass, string uses heap memory and bunch of auxillary members which makes it costly to copy and store.

enums helps to narrow amount of possible inputs. So you have 6 colors with predefined names and you shouldn't fear that someboby will pass something like "Olive Gold"

c = color::WHITE; wouldn't make any sense for me, because WHITE is not a value but a variable...
WHITE is a value. When you define enum, you are defining new type with limited range of values.
Last edited on
What I have now is a "struct" c with 4 members (BLACK, WHITE, BLUE, RED)

An enum is not a struct.
An enum variable is basically an integer , which can have values defined by the enum.
So "color" "c" can have a value BLACK , WHITE &c.
1
2
3
enum color {BLACK = 0, WHITE = 42, BLUE = 3, RED = 14};
color c;
c = BLACK;
Last edited on
enumerations are integer types and are used to define constants.
1
2
enum color {BLACK, WHITE,BLUE,RED};
//BLACK = 0,...,RED = 3 


to check which is which, you can simply use the integer values or the constants themselves,
1
2
3
4
if(c == 0) // if(c == BLACK)
{
   //do stuff
}
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class car
{	

public:
	enum color { BLACK, WHITE, BLUE, RED };
	
	car(color col)
	{
		c = col;
	}

	color GetColor()
	{
		return c;
	}

private:
	color c;
	int a, b;
};

int main()
{
	// setting in the ctor, but you could write a set method
	car myCar(car::color::BLACK);
	car::color myColor = myCar.GetColor();

	return 0;
}


BUT if you're using c++11 i'd use an enum class.

http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html
enums are not structs. color is an enum type. All types has a set of possible values. The bool type have true and false as possible values. unsigned int has 0, 1, 2, 3 and so on. Possible values of type color is BLACK, WHITE, BLUE and RED. Assigning a value to the color variable c can be done the same way as with the int variables a and b.
1
2
a = 1; // assign value 1 to a.
c = BLACK; // assign value BLACK to c. 

Comparing it is also the same
1
2
if (a == 1) { std::cout << "a has value 1"; << std::endl; }
if (c == BLACK) { std::cout << "c has value BLACK"; << std::endl; }
Enum is not a struct and therefore WHITE is not a variable.

Compare to arcane macros:
1
2
3
4
5
#define BLACK 0
#define WHITE 1
#define BLUE 2
#define RED 3
#define color int 

That is less than enumeration. No variables, but no type either.
Hey,

wow thanks for that great support! Ok I think I understood it.
But when I try to do it like mutexe did:
1
2
3
4
car::car()
{
  c = car::color::BLACK;
}

..then I get the following compiler error:
error: 'car::color' is not a class or namespace

If I do it like this instead:
car::BLACK
Compilation works... WHY? BLACK is also not a class or namespace AND this should crash when I create a second enum in the class which also has a const "BLACK"... any solutions??
Last edited on
old unscoped enums dumps their values in enclosing scope.
new scoped enums create new scope

1
2
3
4
5
6
7
8
9
10
11
struct car
{
    enum color {BLACK, /*...*/}
    //Creates values car::BLACK etc

//---

struct car
{
    enum class color {BLACK, /*...*/}
    //Creates values car::color::BLACK etc 
Is your enum declared public or private?
ok, seems like I should use new c++11 standard to avoid problems when using more then one enum type with value BLACK, right?

What I still didn't get so far is why it is possible to write something like this:
car::BLACK
As compiler criticized so far, BLACK is neither a class nor a namespace.. I mean, the scope operator is used to get into scopes and there calling variables/functions... so car::BLACK doesn't make any sense for me... sorry if I'm annoying you but I really want to understand how compiler is managing this...

@mutexe: it's public...
Last edited on
I mean, the scope operator is used to get into scopes and there calling variables/functions...
... inner classes, type aliases, enums...

BLACK is a value of enum. It is inside class car. So to get it, we need to write
1
2
car::BLACK //←Actual value
//↑ Where it is located 


it is like
1
2
3
4
5
6
struct foo
{
    using int_type = uint_least8_t;
};

foo::int_type some_var;


Or: std::vector<int>::iterator //← It's a type
Last edited on
Because, roughly speaking, BLACK, BLUE, RED, etc are just numbers in a class.
BLACK is a value of enum. It is inside class car. So to get it, we need to write

Yeah, and that is exactly what confuses me.. the following is how I understand that and what doesn't make any sense for me:
1
2
3
4
5
6
class car
{
  int i = 5;
};

car::5;  // 5 is also a value of type int... but I don't think that this would work when I try to compile it... would it? 


Because, roughly speaking, BLACK, BLUE, RED, etc are just numbers in a class.

And this is why I more think of them like variables or makros for a value instead of real values... I'm confused...
You do realise the keyword 'enum' actually does something? It's not just there for decoration :)
BLACK is not a number. It is an identifier for a number.

In your example i is the identifier not 5. If it would be static like enum (per definition) it would be car::i;
Last edited on
Ok, I think I understand it... I just don't see a difference between an identifier and a macro... but doesn't matter, I will get used to it the next months :-P

Thank you all for your help! Great forum!
The difference between identifier and macro is that the macro is resolved by the preprocessor. I.e. the compiler isn't involved. The preprocessor doesn't know anything about class, struct, etc. It just replaces one label by another
Pages: 12