I don't know if this is possible to do that. Since one of my function need to pass a function object to c library function and which can only take function pointer.
I created a demo program and I think it is enough to illustrate my purposes:
#include <iostream>
#include <functional>
class Test;
void init_class(std::function<int (int)> fn) {
int (*new_fn)(int) = nullptr; // I can't assign fn to new_fn either <<<<<<<<<<<<
new_fn = fn; // this complains <<<<<<<<<<<<<<<<<<<<<<<<<
std::cout << new_fn(199) << std::endl;
}
class Test {
public:
explicit Test()
: n_(199) {
}
~Test() noexcept {}
int calculate(int val) {
return n_ + val;
}
void run() {
std::function<int (int)> fn =
std::bind(&Test::calculate, this, std::placeholders::_1);
init_class(fn);
}
private:
int n_;
};
void test() {
Test a;
a.run();
}
int main(int argc, const char *argv[]) {
test();
return 0;
}