Binding a class member function to boost::function














































Binding a class member function to boost::function



Explanation: 

When calling such a function, the first parameter passed indicates the particular object for which the function is called. Therefore, the first parameter after the open parenthesis inside the template definition must be a pointer to that particular class. The remaining parameters denote the signature of the corresponding member function.

Code:

#include <boost/function.hpp> #include <functional> #include <iostream> struct world { void hello(std::ostream &os) { os << "Hello, world!\n"; } }; int main() { boost::function<void(world*, std::ostream&)> f = &world::hello; world w; f(&w, std::ref(std::cout)); }


Comments