Original Post
I've been wondering how I would go about implementing button callbacks within my GUI system. The way I'd like to do it would to have my button class with the appropriate SetCallback() function that would set the function to call when the button is pressed. button.h Now the whole GUI system will reside within another Application class, and this is where the Button and its callback function will be stored. application.h application.cpp Ideally this is what the source would look like. When the Button recieves a click message it calls its callback, in this case PopUpHello. Now I know function pointers will be involved in here somehow, but I'm not too sure how to get the functionality I want. Any examples of function pointers I've seen have had the function to point to either be a (1)non-member function or (2)static member function or (3)specifed the name of the class in the function pointer prototype. Option 1 and 2 do not work because the callback will need access to the member data of Application, and option 3 will not work because the name of the class holding the callbacks is not known (won't always be Application.) Has anyone found a solution to this either using function pointers, or another method?
class Button
{
public:
// Constructor yada yada
SetCallback( <function pointer syntax> ); // Sets callback of this Button
};
class Application
{
public:
Application();
// Other functions
void PopUpHello(); // The function to use as callback
private:
Button myButton; // Our button
}
#include "application.h"
Application::Application()
{
// Attach the callback function to the button
myButton.SetCallback(&PopUpHello);
}
void PopUpHello()
{
MessageBox("Hello!");
}