Hello!
I have an enum, that looks like this:
enum Suit {Clubs, Diamonds, Hearts, Spades};
I want to overload the increment operator, so I can easily loop over these four dudes.
When the variable is Clubs, Diamonds, or Hearts there no issue. Its the Spades condition that is giving me a little trouble.
My first instinct was to define it so that when the variable is spades, incrementation sets it equal to Clubs. The problem is that this seems to make it impossible to loop over the 4 values in the enum.
If I do something like
for(Suit i=Clubs;i<Spades;++i)
{cout<<i<<endl;}
then my output only goes to Hearts.
If i do
for(suit i=Clubs;i<=Spades;++i)
{cout<<i<<endl;}
then my output just loops forever!
So, I can obviously think of a few workarounds for this... I'm just not sure what the idiomatic C++ thing do to.
Should I redefine incrementation so that attempting to increment a Spade results in a Spade? or maybe throws an exception?
To reiterate: I can definitely think of a few hacky ways to fix this issue. I just want the guidance of experienced programmers to tell me what they think would be the most "normal" way to solve the problem.