c++ - Overloading increment operator, looping, and edge cases -


i have enum, looks this:

enum suit {clubs, diamonds, hearts, spades}; 

i want overload increment operator, can loop on these 4 dudes.

when variable clubs, diamonds, or hearts there no issue. spades condition giving me little trouble.

my first instinct define when variable spades, incrementation sets equal clubs. problem seems make impossible loop on 4 values in enum.

if like

for(suit i=clubs;i<spades;++i)      {cout<<i<<endl;} 

then output goes hearts.

if do

for(suit i=clubs;i<=spades;++i)     {cout<<i<<endl;} 

then output loops forever!

so, can think of few workarounds this... i'm not sure idiomatic c++ thing to.

should redefine incrementation attempting increment spade results in spade? or maybe throws exception?

to reiterate: can think of few hacky ways fix issue. want guidance of experienced programmers tell me think "normal" way solve problem.

you add enum values start , termination conditions, , alternative ++ doesn't cycle beginning.

enum suit { firstsuit, clubs = firstsuit, diamonds, hearts, spades, allsuits };  ( suit = firstsuit; != allsuits; = iterate_suits( ) ) 

since for , while loops check condition before executing, there no way end execution in middle of cyclic range without additional variables or flow control. do while loop works best in case.

suit iter_suit = my_suit; // iterate on suits beginning my_suit. { } while ( ++ iter_suit != my_suit ); 

in case, don't need firstsuit , allsuits.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -