简单的回调函数理解

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

相关推荐
sunshine22 girl23 分钟前
Java学习六 Java常见API-1-Radom随机数
java·开发语言·学习
可乐鸡翅yeah_27 分钟前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
潼心1412o43 分钟前
C++初阶(长期更新)第6讲:类和对象(下)
开发语言·c++·算法
夜晚回家1 小时前
Python 零基础入门 | 输入的数字为什么不能直接算
开发语言·python
时间的拾荒人1 小时前
Qt 事件机制全解析:从事件处理到事件过滤器
开发语言·qt·面试
-森屿安年-1 小时前
按身高排序
c++·算法
于指尖飞舞1 小时前
java面试题总结
java·开发语言
(Charon)2 小时前
【C++面试】vector底层原理:扩容、拷贝移动与迭代器失效
c++·面试
zhangzeyuaaa3 小时前
深入理解 Ruby 运算符:本质、分类、坑点与重载实战
开发语言·前端·ruby
一木 之林3 小时前
DeepSeek Agent 开发(一)
开发语言·前端·javascript