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...
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.
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;
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;
}
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; }
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??
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...
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
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