Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a class member function as a function parameter

Tags:

c++

syntax

I have 2 C++ Class questions:

The 1st question is: How can I make it so I can pass a class member function as a parameter in another function & how can I then run/call that function? And how can I do the same with a class static function. It maybe easier to understand my question by looking at this code:

class DebuggingManager
{
    string testLog;

    bool test1()
    {
         // run test & return whether it passed or failed
    }    

    static bool test2()
    {

    }

    // How can I call a member function?
    void catalogueTest( string testName, bool DebuggingManager::*nMemberFunction )
    {
        testLog += "Status of " + testName + ": " + ((*)nMemberFunction()) + "\n"; 
    }

    // How can I call a static function?
    void catalogueTest( string testName, bool DebuggingManager::*nStaticFunction )
    {
        testLog += "Status of " + testName + ": " + DebuggingManager::nStaticFunction() + "\n"; 
    }

    // how do I pass a member function or a static function as a parameter in another function 
    bool runTests()
    {
         catalogueTest( "Test of member functin", test1() );
         catalogueTest( "Test of static functin", test2() );
    }

};

The 2nd question is: Is it bad(or dangerous) practice to call a class member (or static) function indirectly like the above. I have a feeling this is really bad C++ practice?

EDIT: Implementing advice Thanks for the reply, I have attempted to implement that advice, its alot to get my head around though, would this be correct?

    // I have a feeling that ParameterList is incorect, would I pass the implicit obj as a parameter or is it done automatically like in normal object function calls?
    typedef bool (DebuggingManager::*MemberPointerType)(ParameterList); 

    void catalogueTest( tstring testName, DebuggingManager* obj, MemberPointerType *nMemberFunction )
    {
        debugLog += _T("Status of ") + testName + _T(": ") + (obj->*nMemberFunction)() + _T("\r\n");
    }

    void catalogueStaticTest( tstring testName, bool DebuggingManager::nStaticFunction )
    {
        debugLog += _T("Status of ") + testName + _T(": ") + nStaticFunction + _T("\r\n");
    }
like image 793
user593747 Avatar asked Aug 28 '11 01:08

user593747


Video Answer


2 Answers

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.

like image 180
Nicol Bolas Avatar answered Sep 29 '22 10:09

Nicol Bolas


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));
like image 27
healthycola Avatar answered Sep 29 '22 11:09

healthycola