使用两个脚本简单理解回调函数。简单来说,将函数B当做另一个函数A的参数进行调用。
Demo1
使用当前通用的std::function<>形式和std::bind形式。
#include <iostream>
#include <functional> // 包含 std::function 和 std::bindusing 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
添加线程执行。
#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函数时的实参即可,更为的灵活。