functor - Is it possible to make a function pointer with parameters in C++ -
i'm trying capture function pointer hand functor , don't understand why can't.
functor class:
template <class c> class myfunctor : public basefunctor { public: typedef long (c::*t_callback)(const long, const char *); myfunctor (void) : basefunctor(), obj(null), callback(null) {} virtual long operator() (const long a, const char *b) { if ( obj && callback ) { (obj->*callback)(a,b); } } c *obj; t_callback callback; };
elsewhere in code:
the function signature long c::func (const long, const char *)
myfunctor funky; funky.obj = this; funky.callback = func;
then error:
... function call missing argument list; ...
why not work?
edit: in working through suggestions below, came across simple solution make particular implementation work.
funky.callback = &c::func;
i'm not 100% sure trying code 2 c++ features easier use function pointers std::function
, std::mem_fn
here's example of using std::function
site.
#include <functional> #include <iostream> struct foo { foo(int num) : num_(num) {} void print_add(int i) const { std::cout << num_+i << '\n'; } int num_; }; void print_num(int i) { std::cout << << '\n'; } struct printnum { void operator()(int i) const { std::cout << << '\n'; } }; int main() { // store free function std::function<void(int)> f_display = print_num; f_display(-9); // store lambda std::function<void()> f_display_42 = []() { print_num(42); }; f_display_42(); // store result of call std::bind std::function<void()> f_display_31337 = std::bind(print_num, 31337); f_display_31337(); // store call member function std::function<void(const foo&, int)> f_add_display = &foo::print_add; foo foo(314159); f_add_display(foo, 1); // store call function object std::function<void(int)> f_display_obj = printnum(); f_display_obj(18); }
this function pointer in code:
typedef long (c::*t_callback)(const long, const char *);
to make std::funciton
do:
std::function<long(const long,const char*)> t_callback = something
;
Comments
Post a Comment