简单的回调函数理解

使用两个脚本简单理解回调函数。简单来说,将函数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函数时的实参即可,更为的灵活。

相关推荐
m0_748236111 分钟前
2024 Rust现代实用教程:1.1Rust简介与安装更新
开发语言·数据库·rust
SomeB1oody8 分钟前
【Rust自学】20.4. 结语:Rust学习一阶段完成+附录
开发语言·后端·学习·rust
五行星辰10 分钟前
Java 读取 Word 模板文档并替换内容生成新文档
java·开发语言·word
旺代34 分钟前
Qt实现简易视频播放器
开发语言·qt
獨枭34 分钟前
如何在 Qt 中添加和使用系统托盘图标
开发语言·数据库·qt
瀛洲客36 分钟前
Qt 支持的动画格式对比,Lottie/APNG/GIF/WEBP
开发语言·qt·lottie动画·apng动画·gif+ webp
JAVA新视界43 分钟前
Spring JDBC模块解析 -深入SqlParameterSource
java·开发语言·数据库·spring boot·sql·spring
__基本操作__1 小时前
方波的基波和谐波详细推导,以及matlab验证[电路原理---2]
开发语言·学习·matlab
别NULL1 小时前
HW机试输入输出格式(C++)
开发语言·c++
阿昆的科研日常1 小时前
Matlab个性化绘图第8期—进度柱状图
开发语言·matlab·可视化·论文插图