enum with additional method

T

toton

Hi,
I have an enum as
enum DitectionType{
X,Y,XD,YD
} ;
Now I want to wrap it with a class which returns orthogonal direction
for it. i.e X -> Y , Y-> X, XD-> YD, YD->XD mapping should be there.
I can think of an static array which stores this mapping, and returns
the value based on index, rather than doing some if else.
Can anybody hint a good way to do it ( the primitive way is an global
static array of 4 element which can return the map like DirectionType
ortho[4] = {Y, X,YD,XD }; But I want a more descent and full proof
solution)

A second thing separately ,not for the above one, I want to make or or
some this enum. So instead of specifing an int, I need so specify a
set among those or / and values (may be using a set )
like I want X|Y to specify either X or Y direction, which should also
be a valid parameter type.


Thanks
abir
 
V

Victor Bazarov

toton said:
I have an enum as
enum DitectionType{
X,Y,XD,YD
} ;
Now I want to wrap it with a class which returns orthogonal direction
for it. i.e X -> Y , Y-> X, XD-> YD, YD->XD mapping should be there.
I can think of an static array which stores this mapping, and returns
the value based on index, rather than doing some if else.
Can anybody hint a good way to do it ( the primitive way is an global
static array of 4 element which can return the map like DirectionType
ortho[4] = {Y, X,YD,XD }; But I want a more descent and full proof
solution)

The only fool proof solution is a switch statement. As soon as I change
the enumerator values in 'DirectionType', a simple lookup table is bust.

enum DirectionType { X=1,Y=7,XD=33,YD=42 };
A second thing separately ,not for the above one, I want to make or or
some this enum. So instead of specifing an int, I need so specify a
set among those or / and values (may be using a set )
like I want X|Y to specify either X or Y direction, which should also
be a valid parameter type.

Not sure I understand this one. Usually, for that you need the values
to be like bit masks, e.g. X==1, Y==2, XD==4, YD==8. The combination
of those is not necessarily going to be represented in your enumeration
by a named value, so you'll need to dance around a bit to massage the
combinations into that (static_cast helps).

Of course, a simple set of unsigned values (with names if you need them)
would probably work just as well.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top