Pass a class member function as a function parameter
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 Debug开发者_如何转开发gingManager::*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");
}
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));
Here is what you want, where X is your class, T is the return of your method and Args are the arguments your function receives.
If your method takes no args, just don't pass them as in test1 and test2.
If your method has void return, than erase the T template and write void where T was on the methods signature.
If you have link errors, remember to define doMember and doStatic in your .h file. By define I mean write the entire function, not just its signature. That's due to template issues.
#include <string>
class DebuggingManager
{
private:
std::string testLog;
bool test1()
{
// run test & return whether it passed or failed
return true;
}
static bool test2()
{
return false;
}
int test3(int a, int b)
{
return a + b;
}
static int test4(int a, int b)
{
return a - b;
}
void catalogueTest(std::string testName, int result)
{
testLog += "Status of " + testName + ": " + std::to_string(result) + "\n";
}
// How can I call a member function?
template <typename X, typename T, typename ...Args>
T doMember(X* obj, T(X::* func)(Args...), Args... args)
{
return (obj->*func)(args...);
}
// How can I call a static function?
template <typename T, typename ...Args>
T doStatic(T func(Args...), Args... args)
{
return func(args...);
}
public:
// how do I pass a member function or a static function as a parameter in another function
void runTests()
{
catalogueTest("Test of member functin", doMember(this, &DebuggingManager::test1));
catalogueTest("Test of member functin", doMember(this, &DebuggingManager::test3, 10, 20));
catalogueTest("Test of static functin", doStatic(DebuggingManager::test2));
catalogueTest("Test of static functin", doStatic(DebuggingManager::test4, 10, 20));
std::cout << testLog << std::endl;
}
};
精彩评论