c++ - Store class member function in a template -


i need store member function pointer somewhere:

void class1::function1(int a, int b) {   ... }  struct parameters {    int a;    int b; }  mystoreclass.store(&class1::function1, parameters); 

and able call later this:

mystoreclass.call(); 

so function called appropriate parameters.

since it's member function suppose should store class1 object pointer too, how can store member function pointer template?

you need parameterize type of object member function on, , pass pointer function. in order call member function need pass pointer object, , parameters call. here sample:

class foo  { public:     void doit (int a, int b)     {        }    };  class bar  { public:     void doitagain (int x, int y)     {        }    };  template <typename obj> void dosomething (obj* that, void (obj::*fnthat)(int,int), int i, int j) {     (that->*fnthat)(i, j);  }  int main() {     foo foo;     bar bar;      void (foo::*fnfoo)(int,int) = &foo::doit;      (foo.*fnfoo)(42,43);      dosomething (&foo, fnfoo, 99, 100);     dosomething (&bar, &bar::doitagain, 200, 300); } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -