This needs a different approach
Steven Schveighoffer
schveiguy at yahoo.com
Mon Apr 28 08:50:08 PDT 2008
"Saaa" <empty at needmail.com> wrote in message
news:fv4q7a$1nde$1 at digitalmars.com...
>
> I have this piece of code:
>
> enum { APPLE, PEAR .. PLUM}
>
> switch (data.type)
> {
> case APPLE:
> groupModule.appleModule.eat();
> break;
> case PEAR:
> groupModule.pearModule.eat();
> break;
> ..
> ..
> case PLUM:
> groupModule.plumModule.eat();
> break;
> default:
> break;
> }
>
> As the function is always the same I'd rather see a lookup iso an
> iteration over all options.. but how?
>
> groupModule.(data.type).eat();
>
> Something like this is both faster and a lot less to code :)
>
Not sure if this works, but what you probably want is a function table:
auto functionArray = [APPLE:&groupModule.appleModule.eat,
PEAR:&groupModule.pearModule.eat, .., PLUM:&groupModule.plumModule.eat];
functionArray[data.type]();
Another option is to store your modules in the groupModule in an array and
just get a common interface returned via a function get:
groupModule.get(data.type).eat();
-Steve
More information about the Digitalmars-d-learn
mailing list