目标是改变事件循环中的行为,具体取决于复选框是打开还是关闭。我能想到的最简单的方法就是在每次运行循环时测试复选框状态。
// if-statement
void action() { /* ... */ }
void someLoop() {
if (checkboxTrue) {
action();
}
// ... other stuff
}
如果使用函数指针,代码会更高效、更简洁还是以其他方式更好?像这样:
// function pointer
void action() { /* ... */ }
void empty() {}
void (*actionPtr)();
void checkboxChanged(int val) {
if (val == 1)
actionPtr = &realAction;
else
actionPtr = ∅
}
void someLoop() {
(*actionPtr)();
// ... other stuff
}