Hi Jacob,
Pointers to methods are a little trickier than pointers to functions. They need to be referred to in the context of an object (or a class if it's a pointer to a static method). In your example, the correct code for the test() method is:
bool testMethod(bool (Test::*methodPtr)(void))
{
return (this->*methodPtr)();
};
Note that the parameter type refers to the method in the context of its class and the call uses an actual object. This syntax is dense, but it's flexible -- it means you can pass a method on a class and call it on any object of that class, not just on "this". Kind of neat.
To pass a method in a function or method, you also need to refer to it in the context of its class. So the correct code for Test::test() is:
void Test::test()
{
bool t = this->testMethod(&Test::someMethod);
}
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|