Pass a class member function as a function parameter

Static member functions of classes are ultimately no different than regular functions. They're really just syntactic sugar; the function is simply has a name that include Classname::.

Non-static members are another matter altogether. There are two important things to remember about non-static member functions (NSMF).

First, every non-static member function has access to the non-static members of the class that they are a member of. This is possible even though you can have two objects of the same class that happen to store different data. If you have two std::string objects, they each store different strings. Executing a find on one string can return a found result in one but not the other.

This is because every NSMF has an implicit this pointer. this refers to, not merely a class, but the actual object upon which that NSMF operates. When you do this:

std::string aString("data");
aString.find("da");

The find function takes a string argument, but it also gets aString as its this. Every time find looks for the members of its class, it will be looking at aString's data.

So let's look at your prospective call of an NSMF:

((*)nMemberFunction())

Where is the object that it gets its this pointer from? Without an object, the NSMF could not access the non-static members of the object, since there is no object for it to find them in. This is not legal.

So, rule #1 about NSMFs: You must call them with an actual instance of the class that the NSMF is a member of (or a derived class thereof). You cannot just take an NSMF pointer and call it like a function pointer; you have to call it on a live object of that type.

Rule #2: the syntax for NSMF pointers is really ugly.

To define a variable (or argument) named arg of NSMF pointer type, you do this:

ReturnType (ClassName::*arg)(ParameterList);

Where ReturnType is the return type of the function, ParameterList is the list of arguments taken by the function, and ClassName is the name of the class to which the NSMF pointer belongs.

Given the ugliness, it is usually best to wrap it in a typedef:

typedef ReturnType (ClassName::*MemberPointerType)(ParameterList);

Thus creating the typedef MemberPointerType, which is a NSMF pointer.

Given an object named object, which is of type ClassName, you would call the member pointer arg as follows:

ReturnType value = (object.*arg)(Params);

Where Params are the arguments you wish to pass. If object is a pointer to a ClassName instead of a reference or a value, then you use object->*arg instead.

One more thing: you must use & to get the member pointer name. Unlike function pointers, NSMF pointers do not automatically convert to member pointers. You have to ask for them directly. So if ClassName has a member called Function that fit the above ReturnType and ParameterList, you would fill arg as follows:

arg = &ClassName::Function;

Rule #3: non-static member pointers are not pointers. Yes, they can be set to NULL (technically, they can be set to 0), but they are not the same thing as a pointer.

Most real C and C++ compilers will allow you to cast a function pointer to a void* and back. The standards consider this undefined behavior, but it's not-entirely-unknown to do this. You absolutely cannot do this with a NSMF pointer, on virtually all C++ compilers. Indeed, the sizeof(MemberPointerType) will likely not be the same size as void*.

So, NSMF pointers are not regular pointers. Do not treat them as such.


In C++ 11 they came up with a way to do that. Read about function and bind operations.

In your case, let's say you wanted to call functions of type test1. (i.e. of form bool FunctionName().

void catalogueTest( string testName, std::function<bool()> myFunction)
{
    testLog += "Status of " + testName + ": " + myFunction() + "\n"; 
}

And call it like this:

DebuggingManager myInstance
myInstance->catalogueTest("TestName", std::bind(&DebuggingManager::test1, myInstance));

Tags:

C++

Syntax