Will Perone

Sometimes you just want to have a pointer to a class member function in a class. Normal function pointers won't work in this case; here's an example of how to do it:
class FooClass
	// define a control function pointer to member function
	typedef float (FooClass::*ControlFuncPtr)(int a, int b);

	// declare the pointer to member function
	ControlFuncPtr ControlPtr;

	// some functions to choose from
	// tip: these can even be virtual and overridden in a derived class
	float func1(int a, int b); 
	float func2(int a, int b);
	float func3(int a, int b);
Now all you need to do is in some member function of FooClass just do:
ControlPtr= &func1;  or func2 or func3
To call it:
FooClass foo;
ret= (foo.*ControlPtr)(whatever, whatever2);
// or from a pointer
FooClass *fooptr;
ret= (fooptr->*ControlPtr)(whatever, whatever2);
Strangely enough, you can actually overload the ->* operator (but not the .* operator); although I don't know why you would ever want to do that. Anyway, to avoid this odd .* and ->* syntax you can have an inlined member function in the class defined as follows:
void Control()  {  (this->*ControlPtr)();  }
Now you can just call FooClass::Control and it will resolve to doing the function pointer and the extra call will compile to nothing from inlining. This is especially useful when you are iterating through some list of objects that have pointers to member functions; STL will die if you don't do this because the .* and ->* are not overloaded for iterators.
For more information see:
Indepth Pointer to Member Functions
More Indepth Pointer to Member Functions
Pointer to Member Function Troubleshooter
1 Comment
nantywele 2012/01/20 Contact Me0 0
You last wishes as have a series of inexpensive racks to the more modern and sui generis racks that are more expensive. <a href=http://allspiceracks.org/Pink-Spice-Rack-132.html>Pink Spice Rack</a>
The reasons why you get the overpriced vigour racks are that they offer you more than just hanging or storing your spices.

<- for private contact