Tag: 面向数据的设计

If语句与函数指针

目标是更改事件循环中的行为,具体取决于是否打开或关闭复选框。 我能想到的最简单的方法就是每次运行循环时测试复选框状态。 // 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 = […]