Pages: [1]   Go Down
Author Topic: C/C++ question on enumerators?  (Read 519 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can I apply Mathematical operations on the enum?

i.e.  some nonsensical code
Code:
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
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This appeases the compiler...

Code:
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,
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 01, 2011, 11:50:03 pm by neurascenic » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
typedef int animal;
#define CAT 1
#define DOG 2
#define Elephant 3
#define Monkey 4
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: