Q:

A good way to implement useable Callbacks in C++

I have a custom Menu class written in C++. To seperate the code into easy-to-read functions I am using Callbacks.
Since I don't want to use Singletons for the Host of the Menu I provide another parameter (target) which will be given to the callback as the first parameter (some kind of workaround for the missing "this" reference).
Registration-Signature
AddItem(string s, void(*callback)(void*,MenuItem*), void* target = NULL)

Example of a Registration
menu->AddItem(TRANSLATE, "translate", &MyApp::OnModeSelected);

Example of a Handler
/* static */
void MyApp::OnModeSelected(void* that, MenuItem* item) {
    MyApp *self = (MyApp*)that;
    self->activeMode = item->text;
}

Is there anything one could consider dirty with this approach? Are there maybe better ones?

A:

Your approach requires the callback functions to either be free functions or static members of a class. It does not allow clients to use member functions as callbacks. One solution to this is to use boost::function as the type of the callback:
typedef boost::function<void (MenuItem*)> callback_type;
AddItem(const std::string& s, const callback_type& callback = callback_type());

Clients can then use boost::bind or boost::lambda to pass in the callback:
menu->AddItem("Open", boost::bind(&MyClass::Open, this));

Another option is to use boost::signals which allows multiple callbacks to register for the same event.

A:

I like your approach. One alternative would be to declare an interface, which is in some sense the "OO equivalent" of a callback:
class IMenuEntry {
public:
    virtual void OnMenuEntrySelected(MenuItem* item) = 0;
};

The registration signature would become
AddItem(string s, IMenuEntry * entry);

And the method implementation
class MyApp : public IMenuEntry {
public:
    virtual void OnMenuEntrySelected(MenuItem* item){
        activeMode = item->text;
    }
}

The interface approach would allow you to avoid the "void * workaround" for the missing this pointer.

A:

You could take a look at using boost::bind.
menu->AddItem(TRANSLATE, 
              "translate", 
              boost::bind( &MyApp::OnModeSelected, this, _1, _2 ));

