#include <iostream>
#include <functional>
#include <string>
using namespace std;
class Test {
public:
Test() {};
~Test() {};
// 设置请求回调
void SetCallBack(std::function<void(const string& stock_code, const string& now_price)> cb) {
now_price_cb_ = cb;
}
// 请求
void Request(short market, const string& code) {
now_price_cb_("222", "111");
}
private:
// 请求回调
std::function<void(const string& stock_code, const string& now_price)> now_price_cb_;
string code_;
};
class Dest {
public:
Dest():controller_(std::make_shared<Test>()) {
controller_->SetCallBack(std::bind(&Dest::OnCallback, this, std::placeholders::_1, std::placeholders::_2));
};
~Dest() {};
void OnCallback(const string& stock_code, const string& now_price) {
cout << "now: " << now_price << endl;
}
void print() {
controller_->Request(market_, code_);
}
private:
std::shared_ptr<Test> controller_;
short market_ = -1;
string code_ = "";
};
int main() {
Dest d;
d.print();
return 0;
}
输出结果: