function - Is there any way to perform something alike std::bind in C? -


so have function of type void (*actiononm)( void * mytypeinstance) , need wrap , pass void (*action)(). such thing possible in c?

you can translate (simple) c++ in c following rules :

  • a c++ class becomes in c struct containing attributes (no methods)
  • c++ methods c functions taking pointer object (here struct) first parameter

here data :

  • a pointer function taking void * parameter , returning void
  • a pointer parameter

but not pass pointer function pointer struct :

typedef struct _binder {     void (*action)(void *param);     void *param; } binder; 

when want bind actiononm(&mytypeinstance) :

binder *bind = malloc(sizeof(binder)); bind->action = &actiononm; bind->param = &mytypeinstance; 

you can pass bound function single parameter bind :

void otherfunct(binder *bind, int otherparam) {     /* ... */     bind->action(bind->param); /* actual call bound function */     /* ... */ } 

and later of course free(bind) :-)

if want closer c++ way, define executor function :

void bind_call(binder *bind) {     bind->action(bind->param); } 

other function become :

void otherfunct(binder *bind, int otherparam) {     /* ... */     bind_call(bind); /* actual call bound function */     /* ... */ } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -