Go Down

Topic: C/C++ question on enumerators? (Read 1 time)previous topic - next topic

neurascenic

Mar 02, 2011, 03:54 am
Can I apply Mathematical operations on the enum?

i.e.  some nonsensical code
Code: [Select]
`enum animal {  CAT       = 1,  DOG       = 2,  Elephant  = 3,  Monkey   = 4}...animal things = ;for (things = CAT; things <= Monkey; things++){  ...}`

Mainly what I question is;    can I do the    "things++"   ?

if so, what would happen if I let it overflow ?     that is to say,   if I did not have the condition of "things <= Monkey"
and just let "things++"   run off?    would it automatically jump back down to CAT  or would it become undefined with a int value of 5+ ?

Thanks!

Chris

#1
Mar 02, 2011, 04:23 am
This appeases the compiler...

Code: [Select]
`for (things = CAT; things <= Monkey; things = (animal)(things + 1) )`

Otherwise you have to override the ++ operator for the animal data-type.

Quote
would it automatically jump back down to CAT

No.

Quote
or would it become undefined with a int value of 5+ ?

Yes,

neurascenic

#2
Mar 02, 2011, 05:06 am
Whoa!

Can you explain this bit

Quote
things = (animal)(things + 1)

?
Specifically what comes after the "="
How and why does this work  ( I tested it and you are correct )  but it seems out of typical language.

Thanks!

Chris

johnwasser

#3
Mar 02, 2011, 05:29 am
I guess the things++ operation (increment the value of 'things') doesn't work so you have to spell it out:

Take the value of 'things':
things
(things + 1)
Treat that value as if it were an 'animal' instead of an integer.  This is called "casting":
(animal)(things + 1)
Store the new 'animal' in the location named 'things':
things = (animal)(things + 1);
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

neurascenic

#4
Mar 02, 2011, 05:46 amLast Edit: Mar 02, 2011, 05:50 am by neurascenic Reason: 1
CB, john

Thanks!

So, allow me to understand a bit more...

(things + 1)   would equate to an integer  (kinda like an auto cast to an int as a simpler form)  , but the resulting integer really isn't an animal unless manually telling it what cast you want?

i.e. ?

things = (animal) 4;  // would result into a monkey ?

Seems as though C would be more flexible if you could work on values of an enum directly.  But oh well.

Thank you again!

Chris
Chris

#5
Mar 02, 2011, 06:06 am
So, allow me to understand a bit more...

(things + 1)   would equate to an integer  (kinda like an auto cast to an int as a simpler form)

Yes, the result is an "int".

Quote
but the resulting integer really isn't an animal unless manually telling it what cast you want?

Yes.

Quote
i.e. ?  things = (animal) 4;  // would result into a monkey ?

Almost correct.  The result is a "Monkey".  Remember, C(++) is case-sensitive.

Quote
Seems as though C would be more flexible if you could work on values of an enum directly.

I think C is less restrictive and allows the use of increment.  C++ has much stricter rules regarding assignment.

Quote
But oh well.

I do like C++ but things like this make me miss ObjectPascal.

johnwasser

#6
Mar 02, 2011, 03:20 pm
If C++ allowed automatic casting of enum types there would be no safety advantage to using the enum over using an int and a bunch of defines (the original C way).  When you create an enumeration you are saying you WANT the type checking to protect yourself from programming errors.  The explicit cast is telling C++ that you KNOW you are doing an unsafe conversion and you promise not to send the enum out of range.  That's probably also why things++ is not allowed: the compiler can't be sure the result will be a valid value.

The following code will replace your enum and eliminate all that pesky type checking:

Code: [Select]
`typedef int animal;#define CAT 1#define DOG 2#define Elephant 3#define Monkey 4`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up