Цитата(Jenya7 @ May 26 2015, 11:24)

совет хороший но как его применить. у каждой команды есть своя функция и соответственно свой указатель. я ищу генерик метод подставить указатель по условию. я могу тупо перебрать их все но хочется как то красиво.
"Сделайте мне красиво"

Вот вам в качестве примера кривая реализация функторов на сях. Может подвигнет к размышлениям (а может к очередной порции страданий, что опять все плохо...).
CODE
//-- pointer to function that performs concrete command processing
struct TCommandFunctor;
typedef int (*PF_CmdProcessor) (const TCommandFunctor* apFunctor);
//-- command functor. encapsulated parameters and a pointer to processing function
struct TCommandFunctor
{
uint32 arg1;
uint32 arg2;
uint8 arg3;
uint16 arg4;
PF_CmdProcessor pCmdProcessor;
};
int DoCallFunctor(const TCommandFunctor* apFunctor)
{
return apFunctor->pCmdProcessor(apFunctor);
}
//-- define command processor functions
int ProcessCmd1(const TCommandFunctor* apFunctor)
{
uint32 res1 = apFunctor->arg1 + apFunctor->arg2;
return (res1 > 733);
}
int ProcessCmd2(const TCommandFunctor* apFunctor)
{
//-- do something else
uint32 res1 = apFunctor->arg3 > apFunctor->arg1;
return res1;
}
//-- define and populate functors
TCommandFunctor cmdFunctors[2] =
{
{1, 2, 3, 4, ProcessCmd1},
{11,22,23,24, ProcessCmd2}
};
void DoWhatever()
{
for(int i=0; i<2; ++i)
{
DoCallFunctor(&cmdFunctors[i]);
}
}
Сообщение отредактировал CrimsonPig - May 26 2015, 11:48