Vorrei dichiarare una funzione che restituisce un puntatore a una funzione dello stesso tipo.Dichiarazione ricorsiva del puntatore funzione in C
vorrei usarlo per implementare macchine a stati come quello qui sotto:
typedef event_handler_t (*event_handler_t)(event_t*); // compilation error
event_handler_t state2(event_t* e);
event_handler_t state1(event_t* e) {
switch(e->type) {
//...
case SOME_EVENT:
return state2;
//...
}
}
event_handler_t state2(event_t* e) {
switch(e->type) {
//...
case OTHER_EVENT:
return state1;
//...
}
}
//...
event_handler_t event_handler;
//...
event_handler(&e);
//...
riesco a risolvere l'errore compliation utilizzando strutture come segue:
typedef struct event_handler {
struct event_handler (*func)(event_t *);
} event_handler_t;
Ma questo fa ritorno statment più complicato:
event_handler_t state2(event_t* e) {
{
event_handler_t next_handler = {NULL};
switch(e->type) {
//...
case OTHER_EVENT:
next_handler.func = state1;
break;
//...
}
return next_handler;
}
Mi chiedo se c'è un modo migliore per creare tali puntatori di funzione in c.
Vedere anche: [SO 816356] (http://stackoverflow.com/questions/816356/how-can-i-typedef-a-function-pointer-that-takes-a-function-of-its-own -type-as-an) –