Go Down

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

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

Coding Badly

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,

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

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
Add the value 1:
       (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: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#4
Mar 02, 2011, 05:46 am Last 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

Coding Badly

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

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: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy