This needs a different approach
Saaa
empty at needmail.com
Mon Apr 28 10:26:07 PDT 2008
>>
>> 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]();
Ok, that works; I think :) although the array would become very large.
>
> Another option is to store your modules in the groupModule in an array and
> just get a common interface returned via a function get:
Erm, how does one do this?
>
> groupModule.get(data.type).eat();
>
> -Steve
>
>
More information about the Digitalmars-d-learn
mailing list