The C++ utilities library provides us with just what we need in order to solve this pickle elegantly:
std::function type is a general purpose polymorphic function wrapper. Amongst many other things it supports, it can store the member function pointers and call them. Let's take a look at a minimal example of using it:
#include <functional> // Defines std::function & std::bind. ... std::function<void(void)> foo = std::bind(&Bar::method1, this);
In this case, we're instantiating a function wrapper called "
foo", which holds a function with the signature
void(void). On the right side of the equals sign, we use
std::bind to bind the member function "
method1" of the class "
Bar" to the
foo object. ...