简单的回调函数理解

使用两个脚本简单理解回调函数。简单来说,将函数B当做另一个函数A的参数进行调用。

Demo1

使用当前通用的std::function<>形式和std::bind形式。

C++ 复制代码
#include <iostream>
#include <functional> // 包含 std::function 和 std::bind

using namespace std;

// 普通函数
void myCallbackFunction(int value, string name)
{
    cout << "Callback called with value: " << value << ", name: " << name << endl;
}

// 执行函数
void executeCallback(std::function<void(int, string)> callback, int data, string name)
{
    cout << "Executing callback..." << endl;
    callback(data, name); // 调用回调
}

int main()
{
    string name = "tao";
    int value = 42;
    // std::function<void(int, string)> 通用的函数包装器类型
    // std::function<void(int, string)> 函数适配器, 将函数与部分参数绑定起来,生成一个新的可调用对象
    // std::placeholders::_1 是占位符,表示在调用 callback 时,第一个参数将被动态传递; 可以先占位, 也可以直接传递
    // std::function<void(int, string)> callback = std::bind(myCallbackFunction, std::placeholders::_1, std::placeholders::_2);
    std::function<void(int, string)> callback = std::bind(myCallbackFunction, value, name);
    executeCallback(callback, value, name);

    // 方法二:直接传递普通函数
    executeCallback(myCallbackFunction, 42, name);
    return 0;
}

Demo2

添加线程执行。

C++ 复制代码
#include <iostream>
#include <functional>
#include <thread>
#include <chrono> // 模拟延时
#include <string>

using namespace std;
// 总体来说, 进行解耦合,将回调函数作为参数传递给需要的地方,而作为回调函数的本身可以有无数个功能不同的函数, 更加的灵活

// 普通回调函数
void myCallbackFunction(int value, string name)
{
    cout << "Callback executed: value = " << value << ", name = " << name << endl;
}

// 模拟异步操作
void asyncOperation(std::function<void(int, string)> callback, int data, string name)
{
    cout << "Starting async operation..." << endl;

    // 模拟耗时操作(例如网络请求、文件处理等)
    std::this_thread::sleep_for(std::chrono::seconds(10)); // 延时2秒

    // 操作完成后调用回调函数
    cout << "Async operation completed!" << endl;
    callback(data, name); // 调用回调函数
}

// 模拟事件触发器
void eventTrigger(std::function<void(int, string)> callback)
{
    cout << "Waiting for event to trigger..." << endl;

    // 模拟事件触发(例如点击按钮,满足条件等)
    std::this_thread::sleep_for(std::chrono::seconds(20)); // 延时3秒
    int eventData = 100;                                   // 模拟事件产生的数据
    string eventName = "Event_A";

    cout << "Event triggered!" << endl;
    callback(eventData, eventName); // 调用回调函数
}

int main()
{
    // 定义一个回调函数
    std::function<void(int, string)> callback = std::bind(myCallbackFunction, std::placeholders::_1, std::placeholders::_2);

    // 异步操作示例
    cout << "[Main] Starting async operation..." << endl;
    std::thread asyncThread(asyncOperation, callback, 42, "Async_User");
    asyncThread.detach(); // 将线程分离,主线程继续运行

    // 事件触发示例
    cout << "[Main] Waiting for event trigger..." << endl;
    std::thread eventThread(eventTrigger, callback);
    eventThread.join(); // 等待事件触发线程完成

    cout << "[Main] Program completed." << endl;
    return 0;
}

总结

从两个样例中可以看出回调函数的一些特性。首先在A中调用B函数和A中调用回调函数的区别是:使用回调函数时不需要定义B函数。其次,当我需要调用同样参数但不同功能的C函数时,使用回调函数仅需要更改调用A函数时的实参即可,更为的灵活。

相关推荐
workflower17 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
CoderYanger18 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
C++业余爱好者18 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
林杜雨都18 小时前
Action和Func
开发语言·c#
皮卡龙18 小时前
Java常用的JSON
java·开发语言·spring boot·json
火山灿火山19 小时前
Qt常用控件(三)
开发语言·qt
利刃大大19 小时前
【JavaSE】十三、枚举类Enum && Lambda表达式 && 列表排序常见写法
java·开发语言·枚举·lambda·排序
float_六七19 小时前
Java反射:万能遥控器拆解编程
java·开发语言
han_hanker19 小时前
java 异常类——详解
java·开发语言
源码获取_wx:Fegn089519 小时前
基于springboot + vue健身房管理系统
java·开发语言·前端·vue.js·spring boot·后端·spring