Tuesday, November 20, 2007

Little Sugar : Pointers to Member Functions

Pointers to member functions is a feature that not many people use but its good to know .
So lets have a look at it . Lets assume you have a class

class Person ;

typedef void (Person::*PPMF)();

class Person{
public :
static PPMF blahFunction(){
return &(Person::processAddress);
}

private :
Address address;
void processAddress();

};

Here blah function returns a pointer to the member function processAddress .
Now you have the pointer to the member address , even if it private ( its evil to to access private member function like this) you can invoke this member function now on an instance of a person
class.

Person boo;

eg. PPMF pmf = boo.blahFunction();

invoke the member function like this :

(boo.*pmf)();

No comments: