or a C++ programmer, the term "a callback function" means at least three different things: a pointer to a freestanding function
(i.e., a C function), a pointer to a member function
, or a function object
. However, in high level programming, these technical minutiae just get in the way. What you need is a generalized abstraction mechanism that can encapsulate any callable entity regardless of its underlying implementation. The recently standardized std::tr1::function
class template offers exactly that. It's a generic callback mechanism with intuitive and uniform syntax and semantics. This article will show you how to use this class template.
How do you generalize the concept of callbacks while maintaining similar syntax and semantics to function pointers?
Use std::tr1::function class template as a generic wrapper for all callable entities.