0
我想實現一個方法來處理C++中的HTTP請求,但我遇到了一些微不足道的問題。我試圖將函數封裝到兩個不同的類中,當我不知道如何在回調函數作爲非靜態成員函數實現時通過引用傳遞迴調函數時,有什麼建議?如何通過引用傳遞成員回調函數
謝謝!
以下是我的一些實現的:
// my_lib.hpp
#include <functional>
class my_lib {
public:
// A library to handle the HTTP requests
using request_callback = std::function<void(int response_code, const std::string &data)>;
void perform_request(std::string &url, std::string &body, const request_callback &callback);
};
// my_lib.cpp
#include "my_lib.hpp"
#include <string>
void my_lib::perform_request(std::string &url, std::string &body, const request_callback &callback) {
// Do http request...
std::string str("...");
callback(200, str);
}
// myClass.hpp
#include <string>
#include <functional>
#include "my_lib.hpp"
class myClass {
public:
using request_callback = std::function<void(int response_code, const std::string &data)>;
void on_action_callback(int response_code, const std::string &data);
void on_action_endpoint(const std::string &request, const request_callback &callback);
private:
my_lib* lib_instance;
};
// myClass.cpp
#include "myClass.hpp"
void myClass::on_action_callback(int response_code, const std::string &data) {
// do something
}
void myClass::on_action_endpoint(const std::string &request, const request_callback &callback) {
int code = 0;
// TODO: how should I pass the on_action_callback here....
lib_instance->perform_request("a.com", "a", myClass::on_action_callback);
}
總是有'std :: function'。 –