View Single Post
  #3 (permalink)  
Old October 23rd, 2004, 05:20 AM
klep klep is offline
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
Reply With Quote