Hello to all! Help, who can, deal with the specifics of the CEventCallback in the Marmalade SDK.

I created 2 classes, GUIElement (prt) and SceneOne (derived). In the descendant I need to pull one of the methods of the base class on the event of clicking on the button.

But something is failing to create an adequate custom handler ... In the examples in m2kit, in the button section, the developers made such a function in main ():

void btnClicked() { /*some code*/ } 

Ie, the function is not a member of any class, it is simply declared in sight. And in Maine they simply pass it as an argument to:

 btn->SubscribeEvent(BUTTON_CLICK, btnClicked); 

And everything works correctly.

But my program should be a bit more complicated =) I need to create my own handler that belongs to the SceneOne class, but I constantly run into an error.

 argument of type "void (SceneOne::*)(m2dkit::core::CEventArgs *args)" is incompatible with parameter of type "m2dkit::core::CEventCallback" 

I also tried to pass such an option as a callback, but the result did not give because there is no possibility to bring the function to the desired type:

 std::bind(&SceneOne::btnPressed, this, std::placeholders::_1) 

Additional info: Scene - http://prntscr.com/bx9np4 | http://prntscr.com/bx9nz7

UPD: When I make a handler method static - it is accepted correctly in SubscribeEvent, but then you cannot pull NOT static methods from the function static. It turns out there are 2 ways and in both a problem ...

  • One solution to the problem is to pass a pointer to an instance of the class in a static method, and it is no longer a static method to pull through it. - Vladimir Gamalyan
  • And if the static handler itself has the signature void (CEventCallBack) (CEventArgs *), then how can I throw in my additional parameter, which is a pointer? After all, the method will work on a click and will take CEventArgs * args into itself - Roman Lipovskiy
  • And where you can look at CEventArgs can give a link? - Vladimir Gamalyan
  • It seems even easier there, the m_Source field in the args handler, this is your btn like. - Vladimir Gamalyan

1 answer 1

The solution was to pull the link to the parent from the button.

 //definition in .h class static void btnClickHandler(CEventArgs*); btn->SubscribeEvent(BUTTON_EVENT_RELEASED, btnClickHandler); void SceneOne::btnClickHandler(CEventArgs* args) { CButtonEventPressedArgs* arguments = (CButtonEventPressedArgs*)args; CButton* button = (CButton*)arguments->m_Source; SceneOne* parent = (SceneOne*)button->GetParent(); parent->showNextScreen(); } 

Based on this, the answer to the question is the choice in the direction of creating a static method in which we pull non-static functions through the parent.

Thanks for the help, @Vladimir Gamalian