Sometimes functions only differ in a few things you cannot express by using variables, what you really love to do is passing a function that can handle the 'special' case and keep your abstraction nice. In some languages like Scheme, you can pass functions as arguments; in C++, you have to use function pointers.
Function pointers are a good system for C, but not for C++; you have to define a non-class member or static class member... So, we cannot access non-static data members of a class without using global variables.
The solution is creating a wrapper around the function pointers that contains the function pointer and a pointer to the class.
Note: an alternative used by STL are function objects; I don't like this approach since it causes too much type-work .
I'll describe a short guide on how to incorporate/use the function pointer wrappers.
- Add the following statements to your header file:
- (optional) Do a
typedef of the templated function pointer, this makes it much easier to read .
It is recommended to make this
typedef inside your class definition, to prevent polluting your global namespace. I usually place them in the private declarations of my class.
typedef Functor<Test, void, int> Function;
In this example,
Test is the class that contains the function,
void is the return value of the function, and
int is the only argument of the function.
- Define your class, I added a quick sample of a class here:
void Add(int x);
void Mul(int x);
void DoSomething(int x, Function f);
void Test::Add(int x)
value_ += x;
void Test::Mul(int x)
value_ *= x;
void Test::DoSomething(int x, Function f)
In the example, you should note
f(x), this is the calling of the function pointer wrapper. Looks exactly like a regular function call, but the
f is an argument of the function.
- Create the function pointer wrapper:
Function myadd(this, &Test::Add);
Function mymul(this, &Test::Mul);
You create a wrapper object just by passing two arguments to the constructor: the host class of the functions, usually a
this, and the name of the method that the wrapper should use.
You can now pass the function to any method and call it as it was a normal function
The system of the wrapper is based on the usage of templates and operator overloading. Templates are used to make it more generic (i.e., to be able to create a wrapper for any function with a specified number of arguments). The operator overloading is used to simulate a regular procedure call, we overload the
operator() to achieve this.
The source code only contains a wrapper for 1 and 2 arguments. If you need more, you'll need to extend the classes or pass a
class that contains the data as single argument.
- Compatibility with
const member functions
- Create an example project
- Create an inline version for compatibility with VC++ 6 or lower.
Kenny Moens is a student computer science at Vrije Universiteit Brussel (VUB), a university located in Brussels, Belgium.
He's been programming for about 10 years with experience in Pascal, Basic, Perl, PHP, C, C++, Java, Scheme, Lisp and Smalltalk.