196. Function Pointer

Question.3

A state machine uses an array of function pointers:

using Handler = void (*)();

void idle()  { /* ... */ }
void run()   { /* ... */ }
void fault() { /* ... */ }

Handler state_table[] = { idle, run, fault };

int current = 1;
state_table[current]();  // Execute current state

What is the advantage over a switch-case?

Need Help? Refer to the Quick Guide below

Select Answer

Restart quiz!