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");
}
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::stringobjects, they each store different strings. Executing afindon one string can return a found result in one but not the other.This is because every NSMF has an implicit
thispointer.thisrefers to, not merely a class, but the actual object upon which that NSMF operates. When you do this:The
findfunction takes a string argument, but it also getsaStringas itsthis. Every timefindlooks for the members of its class, it will be looking ataString‘s data.So let’s look at your prospective call of an NSMF:
Where is the object that it gets its
thispointer 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
argof NSMF pointer type, you do this:Where
ReturnTypeis the return type of the function,ParameterListis the list of arguments taken by the function, andClassNameis the name of the class to which the NSMF pointer belongs.Given the ugliness, it is usually best to wrap it in a typedef:
Thus creating the typedef
MemberPointerType, which is a NSMF pointer.Given an object named
object, which is of typeClassName, you would call the member pointerargas follows:Where
Paramsare the arguments you wish to pass. Ifobjectis a pointer to aClassNameinstead of a reference or a value, then you useobject->*arginstead.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 ifClassNamehas a member called Function that fit the aboveReturnTypeandParameterList, you would fillargas follows: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, thesizeof(MemberPointerType)will likely not be the same size asvoid*.So, NSMF pointers are not regular pointers. Do not treat them as such.