当前位置: 动力学知识库 > 问答 > 编程问答 >

callback - 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 (i.e. 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;
分享给朋友:
您可能感兴趣的文章:
随机阅读: