Re: enum and operator++
On Jan 20, 6:35 pm, Richard Herring <junk@[127.0.0.1]> wrote:
In message
<880512e7-32cc-4ee4-8b71-328ee3241...@30g2000yqu.googlegroups.com>,
Saeed Amrollahi <amrollahi.sa...@gmail.com> writes
Dear all
I am struggling with a problem that's simple per se.
I have an enum and a prefix ++ operator:
enum Dir { North = 1, East = 2, South = 3, West = 4, Max_ = 5 =
}; //
clockwise
Dir operator++(Dir d)
{
Dir dd;
switch (d) {
case North:
dd = East;
break;
case East:
dd = South;
break;
case South:
dd = West;
break;
case West: // wrap
dd = North;
break;
};
return dd;
}
void f()
{
for (Dir d = North; d < Max_; ++d)
// do something
}
The problem is: in the above loop the ++ doesn't work,
indeed the loop is infinite.
The ++ works perfectly. You've defined it to cycle through North, East=
,
South, West, and that's exactly what it does. No amount of
"increment"-ing will ever produce Max_.
If you replace // do something with
output statement, it prints 1 forever.
It is really annoying.
please throw a ligth.
Decide what you really want the increment operator to do. Should it
cycle, or just step once through the range? It can't do both, so if you
need both behaviours perhaps you should consider using named functions
instead of operator++.
--
Richard Herring- Hide quoted text -
- Show quoted text -
Hi Richard
I removed the wrap item from my code.
Thanks,
-- Saeed