回调函数--CallBack


            老师的要求 

9:00 eat
9:00 -11:00 work, 让学生自己安排工作
21:00 run



            学生执行要求 

9:00 eat
9:00 -11:00 work,学生在此时间段想起来要写 code
21:00 run


点击查看代码
#include<iostream>

using namespace std;
//函数func
void run() {
    cout << "在 跑步 !!" << endl;
}

void eat() {
    cout << "在 吃饭 !!" << endl;
}

void work();
void talk();

//函数指针类型FUNC_PTR, 指向函数func()的
typedef void (*FUNC_PTR)();

// 老师的要求
void Urge(string user, FUNC_PTR behave) {
    cout << user;
    behave();
}
int main() {
    cout << "9:00 AM" << endl;
    Urge("jren", eat);
    // callback,回调函数
    cout << "10:00 - 11:00 AM" << endl;
    Urge("jren", work);
    cout << "9:00 PM" << endl;
    Urge("jren", run);

    return 0;
}
// 学生的安排
void work() {
        cout << "在 code !!" << endl;
}
posted @ 2022-06-18 02:11  locker_10086  阅读(13)  评论(0编辑  收藏  举报