简体   繁体   中英

FLTK C++ Callbakc issue

I am trying to pass this pointer as an argument to my callback() , but it seems like the behaviour is not same as I expect.

I am trying to pass the caller object as my user_data (ie 2nd argument of callback() ), apparently it fails!

Within SaveSettings() method, if I type cast void*)obj to (SettingsGui*) , and call foo() , it behaves as expected (so, I am able to pass a pointer to calling SettingsGui obj?) , but when I try to access vector<string> Order (which is filled with string objects within SettingsGui::Show() method), it fails.

Is my assumption of "successfully passing a pointer to calling SettingsGui object " wrong? If so, why and how can I fix it?

// Gui for Application Settings
class SettingsGui {
    vector<string> Order;
public:
    const char foo() { return "Test"; }

    void Show() {
        ...
        Order.size(); /* it is 44 here */
        ...
        // Save Button
        Fl_Button *SaveButton = new Fl_Button(...);
        SaveButton->callback(SaveSettings, this);
        ...
    }

    static void SaveSettings(Fl_Widget *w, void *d){
        SettingsGui *T = (SettingsGui*)obj;
        fl_alert(T->foo()); /* this works */
        char buf[32];
        fl_alert(itoa(T->Order.size(),buf,10)); /* return -1 */

    }
};

// Main Application Window
class MainApp {
public:
    void Show(){
        ...
        ...
        Fl_Button *flButton_settings = new Fl_Button(...);
        flButton_settings->callback(OpenSettingsGui);
    }

    static void OpenSettingsGui (Fl_Widget *w, void *d) {
        SettingsGui p;  
        p.ReadSettings(...);
        p.Show();   
    }
};

T->foo() works because it doesn't access anything within the class

T->Order.size() doesn't work because T is being cast to obj. It should be cast to d.

static void SaveSettings(Fl_Widget *w, void *d){
    SettingsGui *T = (SettingsGui*)d;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM