使用std::function 回调函数
#include <iostream>
#include <functional>

using namespace std;

void readFromFile(const std::string& filename, std::function<void()> callback) {
  // 读取文件
  // ...

  // 文件读取完成后调用回调函数
  std::cout << "Hello, world!\n";
  callback();
}

void onFileRead() {
  std::cout << "File read completed." << std::endl;
}

int main() {
  std::string filename = "example.txt";
  readFromFile(filename, onFileRead);
  return 0;
}

 

posted on 2024-02-27 08:43  Freedom_lyx  阅读(2)  评论(0编辑  收藏  举报