December 2015
Beginner to intermediate
522 pages
11h 21m
English
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. ...
Read now
Unlock full access