Standard function wrapper
The C++ utilities library provides us with just what we need in order to solve this pickle elegantly: std::function
and std::bind
. The 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. The second argument, because this is a member function pointer, is the instance of the class that is having its method registered as a callback. In this case, it has to be an instance...