Browse DevX
Sign up for e-mail newsletters from DevX


Use Function Adapters to Extend Generic Algorithms' Usage

Binding a member function to an object or a pointer can be done with the standard set of function adapters. Learn how in this month's solution.

As useful as pointers to member functions are, in certain situations—for example when using STL algorithms—you need to use more sophisticated constructs that wrap bare pointers to members and transform them into a slightly different beast.

STL algorithms that take a function argument don't care whether it's a real function, a member function, or a function object, so long as they can use the notation f() to call it. The problem is that you can't call a member function directly; you need to call it for a pointer or an object:

(p->*pmf)();  //invoke through a pointer
(obj.*pmf)(); //invoke through an object
How is it possible, then, to pass only a pointer to a member and have the algorithm call it?

Use the standard set of function adapters to bind a member function to an object or a pointer.

Thanks for your registration, follow us on our social networks to keep up-to-date