I have a custom menu class written in C ++. To split the code into easy-to-read functions, I use callbacks.
Since I don't want to use singletones for the menu host, I provide another parameter (target) that will be passed to the callback as the first parameter (some kind of workaround for the missing "this" link).
Registration-Signature
AddItem(string s, void(*callback)(void*,MenuItem*), void* target = NULL)
Registration Example
menu->AddItem(TRANSLATE, "translate", &MyApp::OnModeSelected);
Handler example
void MyApp::OnModeSelected(void* that, MenuItem* item) {
MyApp *self = (MyApp*)that;
self->activeMode = item->text;
}
Is there anything that could be considered dirty with this approach? Maybe better?
source
share