Programming Tips & Tricks

Home Categories: C# | C++ | General | Other

C++ Tips & Tricks: Function Pointers to Member Functions

Function pointers are a neat concept, since they basically allow the storage and passing around of functions.
But when i was first experimenting with them, i had a hard time finding information about how to handle pointers to member functions of classes, instead of ordinary functions (note however, that this was many years ago, and i guess that today's C++ tutorials include them), so here's how to use them:

First the easy case: pointers to static member functions. Since they don't depend on an object instance, they look like ordinary function pointers.
Assume we have some class with a static function:
class myclass
static void myfunc(int x) { std::cout << x << std::endl; }

Now we declare a type for a pointer to the static member function:
typedef void ( *STATICFUNC ) ( int );

Now assigning a function pointer and calling the function with it is easy:
void TestStatic()
STATICFUNC f = myclass::myfunc; // assign address of memberfunction
f ( 123 ); // call the function via function pointer

Now to the interesting part: function pointers to non-static member functions.
First assume we have again our 'myclass', only that myfunc isn't declared static this time.
The type of a function pointer to it now looks like this:
typedef void ( myclass::*FUNC ) ( int );

The interesting part of the syntax here is that the asterisk comes after the class name.
To use the function pointer, we have to instantiate an object of type myclass, then we can create a function pointer and call the function with it:
void TestMemberFunc ()
myclass obj; // instantiate myclass
FUNC f = &myclass::myfunc; // assign address
( obj.*f ) ( 123 ); // and call it

The important thing in the syntax here are the parentheses around obj.*f, which basically bind the pointer to the object whose member function we are calling.