View Single Post
Old Feb 9th, 2008, 1:04 PM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
Re: Pointer to member function

Unless the function you will call isn't static you have to pass a pointer to the object too. Something like this maybe. This code may not compile but you probably get the idea. Not sure about the member function syntax.

class ButtonCallbackListener
{
public:
};

class Button
{
public:
	Button(ButtonCallbackListener* object, void (ButtonCallbackListener::*functionPtr)());

	void onPressed() { mObject->(*mFunctionPtr)(); }

private:
	ButtonCallbackListener* mObject;
	void (ButtonCallbackListener::*mFunctionPtr)();
};

class MyClass : public ButtonCallbackListener
{
public:
	MyClass() { mButton = new Button(this, &MyClass::callback); }

	void callback() {}

private:
	Button* mButton;
};
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote