I think what Bubba is getting at is that callbacks in C++ are handled by abstract interfaces, not function pointers. Example:
Code:
class Action
{
public:
virtual ~Action() {} // Standard for abstract bases
virtual void Do( Widget &actor ) = 0;
};
Now, the various actions you want to associate with events are just subclasses of Action with specific implementations of the Do() function. For instance:
Code:
class QuitAction : public Action
{
public:
virtual void Do( Widget & ) { QuitTheApp(); }
};
The widget (button in your example) might have a way of registering some action to execute when it is clicked:
Code:
class Button
{
...
void setAction( Action *action ) { myAction = action; }
...
};
...
Button *quitButton = new Button;
quitButton->setAction( new QuitAction() );
And the button's onClick() function would just dispatch to the action:
Code:
void Button::onClick( ... )
{
myAction->Do( *this );
}
The purpose of the *this parameter is just so the action can figure out which object triggered it. It might not be necessary depending on how you choose to implement the actions.
I can't help but point out that these ideas have been rehashed and reimplemented for decades. I've felt the urge to reimplement it all myself, too, and I wouldn't discourage you from trying. Just be aware that your final design will probably be a mess, not for lack of trying.
(I guess it goes without saying that my examples don't demonstrate proper resource management)