assinging a c++ function object to c function pointer -
assinging a c++ function object to c function pointer -
this question has reply here:
convert c++ function pointer c function pointer 6 answersi don't know if possible that. since 1 of function need pass function object c library function , can take function pointer.
i created demo programme , think plenty illustrate purposes:
#include <iostream> #include <functional> class test; void init_class(std::function<int (int)> fn) { int (*new_fn)(int) = nullptr; // can't assign fn new_fn either <<<<<<<<<<<< new_fn = fn; // complains <<<<<<<<<<<<<<<<<<<<<<<<< std::cout << new_fn(199) << std::endl; } class test { public: explicit test() : n_(199) { } ~test() noexcept {} int calculate(int val) { homecoming 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(); homecoming 0; }
i had same problem - @ link below, there few interesting answers: convert c++ function pointer c function pointer in brief can't assign function pointer non-static c++ fellow member function. can create static or global function makes phone call instance parameter, @ link above more details.
c++ function stl
Comments
Post a Comment