Function Callbacks: call an object's method with args

  • 2 Replies

I need to send some arguments in a callback but it doesn't seem to be possible in an object. Basically, I saw that in the callback tutorial:

Code: (cpp) [Select]
void function(tgui::Gui& gui);
button->bindCallback(std::bind(function, std::ref(gui)), tgui::CheckBox::Checked | tgui::CheckBox::Unchecked);

And I wanted to do that:

Code: (cpp) [Select]
p.button->bindCallback(std::bind(&AddObjectDialog::CloseAndSaveCallback, p.object), this, tgui::Button::LeftMouseClicked);
But it doesn't seem to be possible, I get the following error:

Code: (xml) [Select]
'tgui::CallbackManager::bindCallback' : no overloaded function takes 3 arguments
« Last Edit: 17 June 2015, 18:00:58 by Dexario »



  • *****
  • 1631
    • View Profile
    • Texus's Blog
Re: Function Callbacks: call an object's method with args
« Reply #1 on: 17 June 2015, 18:01:44 »
Since the CloseAndSaveCallback is a function inside a class, it looks like this for the compiler:
Code: (cpp) [Select]
void CloseAndSaveCallback(const AddObjectDialog* this, Obj obj)
std::bind takes the following parameters: the function and the parameters. Since "this" is always the first parameter, it should always be before the other parameters in the bind call. The bind call needs to look like this:
Code: (cpp) [Select]
std::bind(&AddObjectDialog::CloseAndSaveCallback, this, p.object)
The bindCallback function has one version where the first parameter is the function and the second one is the 'this' pointer, but this is actually only a shortcut so that you don't have to call std::bind yourself. But if you need a function that has parameters then you must use the normal bindCallback function. So the line should look like this:
Code: (cpp) [Select]
p.button->bindCallback(std::bind(&AddObjectDialog::CloseAndSaveCallback, this, p.object), tgui::Button::LeftMouseClicked);

Re: Function Callbacks: call an object's method with args
« Reply #2 on: 17 June 2015, 18:05:27 »
Thanks !