#include <iostream>
#include <string>
#include <functional>
#include <thread>
using namespace std;
using Func = function<void(int)>; //函数指针 意味着是 void A (int B){} 的函数缩写
class Driver
{
private:
Func func; //函数指针
public:
void registerCallback(Func f) //注册 ,形参是函数指针
{
func = f;
}
void start()
{
cout << "driver..." << endl;
int i = 0;
while(true)
{
func(i);
cout << "driver start:" << i++ << endl;
this_thread::sleep_for(chrono::milliseconds(10)); //睡眠10ms
}
}
};
class App
{
public:
App()
{
Func func = std::bind(&App::callback, this, std::placeholders::_1); //回调函数
Driver driver;
driver.registerCallback(func);
driver.start();
}
void callback(int i)
{
cout << "App callback:" << i << endl;
}
};
int main()
{
App app;
return 0;
}