Topic: Enums


Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 1995/04/14
Raw View
gadget@coho.halcyon.com (Timothy Sharpe) writes:

>Consider:
>enum day
>{monday, tuesday, wednesday, thursday, friday};

>This is interesting in a switch statement:
>switch(dayCurrent) { case monday: ... }

>But is uninteresting as an integer:
>dayCurrent++

>Has someone designed an enum which wraps around (friday+1 = monday) ?

A post-ARM addition to the standard allows operators to be overloaded
for enum types, so you can define the functions:

day operator++(day&,int);   // postincrement form
day& operator++(day&);      // preincrement form

which do what you want.





Author: horstman@sjsumcs.sjsu.edu (Cay Horstmann)
Date: 1995/04/16
Raw View
David Sachs (b91926@fsgm01.fnal.gov) wrote:
: gadget@coho.halcyon.com (Timothy Sharpe) writes:

: >Consider:
: >enum day
: >{monday, tuesday, wednesday, thursday, friday};

: >This is interesting in a switch statement:
: >switch(dayCurrent) { case monday: ... }

: >But is uninteresting as an integer:
: >dayCurrent++

: >Has someone designed an enum which wraps around (friday+1 = monday) ?

: A post-ARM addition to the standard allows operators to be overloaded
: for enum types, so you can define the functions:

: day operator++(day&,int);   // postincrement form
: day& operator++(day&);      // preincrement form

: which do what you want.

I am not sure that is a good thing to do, though. It conflicts with
the more desirable goal of having an STL style iteration protocol:

enum Weekday { monday, tuesday, ..., friday, end, begin = 0 }

and then iterate over all weekdays:

 for (Weekday w = Weekday::begin; w != Weekday::end; w++)

Cay





Author: gadget@coho.halcyon.com (Timothy Sharpe)
Date: 1995/04/14
Raw View
Consider:
enum day
{monday, tuesday, wednesday, thursday, friday};

This is interesting in a switch statement:
switch(dayCurrent) { case monday: ... }

But is uninteresting as an integer:
dayCurrent++

Has someone designed an enum which wraps around (friday+1 = monday) ?

Tim