jeudi 30 mai 2019

callback function is not giving desired output

As per below code snippet shared in coliru link, Each time the user clicks the i’th button, the m_clicks[i] and m_total_clicks should both get incremented. To achieve this, the class MyFrame will register a "on_click-handler" callback with each button. Trying to register button_clicked as the callback doesn’t work.
m_pushbuttons[i].on_click_handler(MyFrame::button_clicked);// Doesn't give expected result

Below is the coliru link for source code. https://coliru.stacked-crooked.com/a/c15865d59b41341f

Here is the expected output based on below test cases for both f1 and f2 objects with print_stats method.

//MyFrame f1(2)
//f1.m_buttons[0].click();
//f1.m_buttons[0].click();
//f1.m_buttons[0].click();
//f1.m_buttons[1].click();
//f1.m_buttons[1].click();

//f1.print_stats();

//MyFrame f2(3);
//f2.m_buttons[2].click();
//f2.m_buttons[2].click();
//f2.m_buttons[2].click();
//f2.m_buttons[1].click();
//f2.print_stats();
// Should print:
// Total Clicks: 4
// Button[0] clicks: 0
// Button[1] clicks: 1
// Button[2] clicks: 3

So basically I need to write a client class so that when instantiated, will create N such buttons (where N is a constructor time parameter). The client class wishes to register a callback to keep track of button clicks but in current implementation, There are couple of issues.

(1) std::vector MyFrame :: m_clicks (2) is initialized statically and initialize value should be same as MyFrame initialized object value instead of static value .

(2) Without declaring static m_total_clicks and m_clicks , Is there any other way to achieve the same.

(3) on line no. 20 ,m_ftr pointer needs to be properly initialized.

Could you suggest any proper design/implementation for this callback implementation.

Aucun commentaire:

Enregistrer un commentaire