cpp//回调函数的简单理解
//例子温度计显示温度。
//有两种方式
// 1. 睡眠5秒,查看一下温度计读数函数,如果度数改变则更新显示。
// 2. 把显示接口暴露给温度计度数函数,由温度计来显示,这样不用一直去访问,浪费切换时间。
// ====== method 1. 轮询 ======
float read(){
return get_temperature();
}
void display(){
float f = read();
print(f);
return;
}
// ====== method 2. callback ======
float history;
// 引入函数指针;
float read( (void)(*function)(void) ){
float f = get_temperature();
if( f != history){
(*function)();
}
return f;
}
// 调用
read(&display);
// plus:
// 中断程序是特殊的回调
本文作者:Santiago Chou
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 无版权要求,都是一些日常话题。 许可协议。转载请注明出处!