您的位置:首页 > 其它

回调函数

2015-07-10 21:41 302 查看
void printHello(int n){//相应回调事件
printf("你好---%d\n", n);
}
void printBye(int n){
printf("再见---%d\n", n);
}
void caller(int n, void(*print)(int)){//相应回调事件
int sum = 0;
for (int i = 1; i <= n; ++i){
sum += i;
}
print(sum);//调用回调函数
}

#define CMD_HELLO 1
#define CMD_BYE 2
int main(void){
while (1){
cout << "请输入命令:1代表迎客, 2代表送客" << endl;
int cmd;
cin >> cmd;
switch (cmd)//触发回调关联事件
{
case CMD_HELLO:caller(1, printHello); break;//注册回调函数
case CMD_BYE:caller(2, printBye); break;//注册回调函数
default:
break;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: