If you're using a C++11 compiler, then it's rather simple. Given this class:
class Class1 {
public:
void function1(int a, int b);
};
you can just prepare a call using std::bind:
#include <functional>
// ...
Class1 class1Obj;
auto myStore = std::bind(&Class1::function1, &class1Obj, 10, 20);
and you can call it later with:
myStore();
// The above has the same effect as:
// class1Obj.function1(10, 20);
You don't actually need a StoreClass at all (it's pretty much what std::bind
does already.)
This is just an example. You obviously need to make sure that the instance of Class1 you're calling Class1::function1() on (class1Obj
in the above example) still exists when you perform the call.
If you're not on C++11, then you can use Boost instead:
#include <boost/function.hpp>
#include <boost/bind.hpp>
// ...
boost::function<void()> myStore = boost::bind(&Class1::function1,
&class1Obj, 10, 20);
the call is the same:
myStore();
In both cases, the type of the function returned by bind
is void ()
. Meaning a function returning void
and taking no arguments (since they've been "swallowed" up by bind.) So if you still need to create a template in order to parameterize the type of member function calls you can store, you only need to do so on the return type of the member functions, not their arguments. For example:
template <typename T>
// ...
std::function<T ()> storedCall;
// or with Boost:
// boost::function<T ()> storedCall;
Note that Boost.Bind and Boost.Function are header-only libraries. That means they're easy to use and don't require you to link against any libraries at all.