Iterating an enum

Hey there,

Basically I want to be able to increment an enum that I created.

So I have:

1
2
enum Type{blah, blah2,blah3};
Type myEnum = blah;


None of the following work (am trying to cycle through options)
1
2
3
4
blah = blah+1;
blah += 1;
blah++;
++blah;


So I thought to try to overload the ++ operator ( http://stackoverflow.com/questions/3106015/how-do-i-increment-an-enum-in-vs-c-6-0 ).

I came up with the following but when I call it from within my game it fails to increment the enum.

1
2
3
4
5
6
// Overload the ++ operator to allow user to cycle forwards through type
inline Type operator++(Type eDOW, int)
{
   const int i = static_cast<int>(eDOW);
   return static_cast<Type>((i + 1) % 4);
}


Has anyone got any ideas with what I am doing wrong?

Or fail that any other ways to go about incrementing it without a massive amount of switch or if statements?

Thank you,
Chazzmundo
Is incrementing an enum necessarily a good idea? A slight change to the ordering of the enum elements could change program behaviour.

EDIT: ++ is an unary operator, but you have given it two parameters. My bad, the second parameter is necessary it would seem.

Also ++ is supposed to change the original value, whereas you have just returned and incremented value, leaving the original value unchanged. You would need a reference parameter to do this correctly.

EDIT EDIT: Forget this bit... EDIT: But you could just define an increment function to increment it, bearing in mind the various points I have made above.
Last edited on
Don't do math on an enum.

How about:

const int blah = 1;
const int blah2 = 2;
const int blah3 = 3;
EDIT: I successfully made a nonmember increment operator for a class, but the same code failed for an enum. As abellia said, math on an enum is not a great idea :P

Is incrementing an enum necessarily a good idea? A slight change to the ordering of the enum elements could change program behaviour.

I think the problem with your operator is that it is defined incorrectly. ++ is an unary operator, but you have given it two parameters (what is the int for?). In fact, I think this operator must be a member function, which I don't think is possible for an enum.

Also ++ is supposed to change the original value, whereas you have just returned and incremented value, leaving the original value unchanged. You would need a reference parameter to do this correctly.

EDIT: But you could just define an increment function to increment it, bearing in mind the various points I have made above.


Well when I tried to compile with just one parameter; it refused. By adding a second parameter it allowed the program to compile :S

I did not consider a reference parameter actually.


Don't do math on an enum.

How about:

const int blah = 1;
const int blah2 = 2;
const int blah3 = 3;

->

EDIT: I successfully made a nonmember increment operator for a class, but the same code failed for an enum. As abellia said, math on an enum is not a great idea :P




It seemed the sensible idea at the time :P

Am I really forced to use a switch statement?

Will this not work in the slightest?

Well what are you trying to do? Perhaps there is a whole different method for your problem.
(To increment an enum, you need this:

1
2
3
4
5
inline Type& operator++(Type& eDOW, int)  // <--- note -- must be a reference
{
   const int i = static_cast<int>(eDOW);
   return static_cast<Type>((i + 1) % 4);
}

)
In case anyone is still wondering, the dummy int parameter on the end it to differentiate the post- and pre-increment operators.
I tried to implement it using my own enum (should be a straight forwards rewording right?), but it did not work.

1
2
3
4
5
6
enum ControlType{BASE, UPPER_ARM, LOWER_ARM, WRIST};
inline ControlType& operator++(ControlType& eDOW, int)  // <--- note -- must be a reference
{
   const int i = static_cast<int>(eDOW);
   return static_cast<ControlType>((i + 1) % 4);
}


I got the following errors:


Error	1	error C2440: 'return' : cannot convert from 'ControlType' to 'ControlType &'	

	2	IntelliSense: initial value of reference to non-const must be an lvalue	



Any ideas?
I'd say it should be:
1
2
3
4
5
6
7
enum ControlType{BASE, UPPER_ARM, LOWER_ARM, WRIST};
inline ControlType& operator++(ControlType& eDOW, int)  // <--- note -- must be a reference
{
   const int i = static_cast<int>(eDOW);
   eDOW = static_cast<ControlType>((i + 1) % 4);
   return eDOW;
}
Thank you guys especially coder777 (it worked perfectly).

For reference to anyone else who wishes to do this, my final code for both iterators is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
enum ControlType{BASE, UPPER_ARM, LOWER_ARM, WRIST};

// Overload the ControlType++ operator
inline ControlType& operator++(ControlType& eDOW, int)  // <--- note -- must be a reference
{
	const int i = static_cast<int>(eDOW)+1;
	eDOW = static_cast<ControlType>((i) % 4);
	return eDOW;
}

// Overload the ControlType-- operator
inline ControlType& operator--(ControlType& type, int)  // <--- note -- must be a reference
{
	const int i = static_cast<int>(type)-1;
	
	if (i < 0) // Check whether to cycle to last item if number goes below 0
	{
		type = static_cast<ControlType>(3);
	}
	else // Else set it to current number -1
	{
		type = static_cast<ControlType>((i) % 4);
	}
	return type;
}



Just as a word, precaution needs to be taken in the -- overload to make sure it does not return a negative number. Cast it as max_items -1 (mine was 3 as you can see).

This works perfectly when I call using:


1
2
3
ControlType t;
t++;
t--;



Thank you everyone :)
Topic archived. No new replies allowed.