Here's a defined enumeration:
enum shipCost {FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50};

Then they do this which I don't understand:
shipCost myShipCost = BOMBER_COST;

My question is why don't you just do the following instead?
int myShipCost = BOMBER_COST;

Edited 5 Years Ago by crapgarden: n/a

It might be the case that a shipCost is represented as an int but it might not. The fact is that the types are not the same (regardless of the underlying representation) and should be treated that way.

It seems like the actual variable 'shipCost' is being used to declare a new variable like you would use 'int', 'char', or 'bool'. Seems strange to me. Is this normal? Would this then mean that myShipCost can't be changed to anything except for one of the values in shipCost?

Also, is an enumeration a constant value or can it change?

Yes. The enum introduces a type. It can be used as any other type syntactically. This nice thing about enums is that the values that are defined can also be used as constants. So something like

enum color { RED = 2, GREEN = 4, BLUE = 8 };

// defining the function
void change_color (color c) { ... }

// calling the function
change_color (GREEN);

Also, if you try to convert from an int value (even one represented by the enum) to a type of enum you should get a compile-time error. So the following should fail

enum foo { FIRST = 2, LAST = 4 };
int main () {
   foo f = FIRST;
   f = 4; // not allowed
   return 0;
}

I'm getting closer I promise... I get that it's a good way to reduce errors in constants in a project but here's what I guess I'm missing the logic in. What's the advantages/disadvantages of the following two examples:

EXAMPLE 1 - ENUMERATION

enum alienDamage {BUG = 5, BIG_BUG = 10, SUPER_BUG = 20, BOSS_BUG = 50, OMEGA_BUG = 100};
alienDamage playerInjuryAmount = BIG_BUG;
playerHealth = (playerHealth - playerInjuryAmount);

EXAMPLE 2 - CONST INT DECLARATION

const int BUGdmg = 5, BIG_BUGdmg = 10, SUPER_BUGdmg = 20, BOSS_BUGdmg = 50, OMEGA_BUGdmg = 100;
playerHealth = (playerHealth - BIG_BUGdmg); //this seems more efficient no?

I'm sure I'm missing something but #2 still seems more efficient and clearer to me. That may very well be due to my noobery. Thank you for helping btw. I appreciate it.

Edited 5 Years Ago by crapgarden: n/a

In your examples there is nothing to gain. Consider the situation of a function call, however

enum legal_values { A = 4, B = 6, C = 8, D = 33, F = 109 };

void some_function (legal_values value) {
   // here you *know* value is one of the defined values
}

void some_other_function (int value) {
   // here you have to check for a match to a proper value
   // which may be lengthy depending on the number of values you have
}

Another use that I find enums to be beneficial for is looping constructs. For instance

enum { A = 0, B, C, D, E, ..., LAST_ELEMENT };

for (int i = A; i < LAST_ELEMENT; ++i) {
   // ...
}

In that case you never have to worry about making sure that some value is adjusted each time the set grows since inserting elements into the enum will automatically adjust the LAST_ELEMENT.

Edited 5 Years Ago by L7Sqr: n/a

...brain chewing...

Ok, ignore the looping thing for now.
The purpose of the function example was to show how using an enum can help restrain the input to a function. Here is more of an extended example

enum valid { A = 1, B = 3, C = 32 };
void fun (valid val) { }

int main () {
    fun (A);
    fun (B);
    fun (C);
    fun (1); // fails compile
    fun (7); // fails compile
    return 0;
}

However, if you didn't use the enum approach you would have to do something like

void fun (int val) {
    if (val != 1 || val != 3 || val != 32)
        // what is a good error action ???
}

int main () {
    fun (1);
    fun (3);
    fun (32);
    fun (1); // passes compile
    fun (7); // passes compile
    return 0;
}

So you can see you can restrict value to your function - constrain user input - at compile time with an enum. Whereas with an int you must do the check yourself and plan some error processing that might be non trivial.

Ah - ok. So you want to use it when you know there are specific sets/groups of things you don't want the values of to change. This reduces the chances for errors.

I guess the only thing still eating at me is this:

When I declare for example: int playerItems, or char itemColor, I can then use them as simply as:

playerItems = 3;
itemColor = g;

When I want to declare an enum: enum shipCost {FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50}; then I CANNOT use it like this:

shipCost = BOMBER_COST;

I need to declare a new type that is based on my above delcared enum, and which only TAKES values FROM THAT ENUM ONLY:

shipCost MyShipCost = BOMBER_COST; //ok.
shipCost MyShipCost = 25 //error compile.

shipCost is the type. So your first example is as if you are saying int = 7; . That makes no sense. An enum can be implicitly converted to an int so either of the following are fine

enum foo { A, B };
int i = A; // implicit conversion, has type int 
foo f = B; // f is a foo type

but the following is not

foo bad = 0; // no implicit conversion from int to foo, syntax error

Yes, since the enum has defined a new type, you cannot say shipCost = BOMBER_COST; It would be equivalent to saying something like int = 25. In both cases you need to define a variable of that type and then set the variable to the value required:

shipCost MyShipCost = BOMBER_COST;
int MyInt = 25;

This question has already been answered. Start a new discussion instead.