To take a member function pointer to MyClass
which returns void and takes no additional parameter declare it like -
void myfunction(void (MyClass::*somefunction)(), MyClass* obj)
{
(obj->*somefunction)();
}
Here void (MyClass::*somefunction)()
means somefunction
is a pointer to member function of MyClass
that takes no additional (except for implicit MyClass*
as first parameter) parameter and returns void
. See this for more about member function pointer.
To make the function more generic like std::thread
declare it as following-
The first one takes any non-member function (aka. free function) of any type
template<class F, class... Args>
void myfunction(F&& f, Args&&... args)
{
f(std::forward<Args>(args)...);
}
and second one takes the member function pointer of any type of any class.
template <class R, class C, class... Args>
void myfunction(R(C::*mf)(Args...), C* pc, Args&&... args)
{
(pc->*mf)(std::forward<Args>(args)...);
}