Posts

Showing posts with the label boost::bind

Using boost::bind to assign functions

Some code samples I have collated in the sample below, that demonstrate how boost::function can be assigned with functors, ordinary functions, class member functions and overloaded class member functions respectively. [code language="cpp"] #include <iostream> #include <boost/function.hpp> #include <boost/bind.hpp> using namespace std; // Class for example 1: functors class int_div { public: float operator()(int x, int y) const { return ((float)x)/y; }; }; // Class for example 2: : accessing functions float average( int values[], int n ) { int sum = 0; for (int i = 0; i < n; i++) sum += values[ i ]; return (float) sum / n; } // Class for example 3: accessing class members class DoStuff { public: void DoThis() { std::cout << "Do this" << std::endl; } void DoThat( std::string message ) { std::cout << message << std::endl; } }; // Class for example 4: overloaded class members class Overload { p...

Using boost::bind as an improved means of calling member functions

This post takes a look at using boost::bind as a means of calling class member functions in an efficient and generic way. It basically summarizes what has already been said at Björn Karlsson's excellent Informit article . Since I found the post useful, I thought it worth reproducing here, using the same status class but containing all the examples and approaches he describes in one program.

A First Stab at boost::bind

Boost::bind is “able to bind any argument to a specific value or route input arguments into arbitrary positions.” It's a means of converting a function into an object that can be copied around and called at a later point, deferred callbacks for example.