简单的回调函数理解

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

相关推荐
肆忆_1 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc