On 22.08.2011, at 12:55, asif saeed wrote:
> Hi Ian,
>
> Thanks for replying.
>
> On Mon, Aug 22, 2011 at 3:41 PM, MacArthur, Ian (SELEX GALILEO, UK) <
> [email protected]> wrote:
>
>>
>>> That's exactly what I wanted to know. But the question
>>> remans: when should I
>>> create member widgets as pointers and when should I create
>>> them as direct
>>> object members (non pointers, that is)?
>>
>
> I tend to always create as pointers then "new" the object into
>> existence, but that's just my coding style; other ways are at least as
>> valid...
>>
>
> Yes, I guess this is a better approach.
Using pointers is probably the easier way. You can only use widgets allocated
on the stack until you leave the function (because when you leave the function,
they will be destroyed). For example, this is fine:
main() {
Fl_Window win(300, 300);
win.show();
Fl::run();
}
But this will simply never show anything usefule because the window is
destroyed before you reach "run":
make_ui() {
Fl_Window win(300, 300);
win.show();
}
main()
make_ui();
Fl::run();
}
As for the order of destruction:
main()
Fl_Window win(300, 300);
// creates the window, all further widgets are added to win
Fl_Button btn(10, 10, 100, 20);
// is atumatically added to win
win.end();
win.show();
Fl::run();
// when you leave the function this happens:
// stack-unwind calls "delete &btn"
// - btn is removed from win
// - btn is destroyed
// stack-unwind calls "delete &win"
// - win destroys all its children (none, because btn was already removed)
// - win is destroyed
}
The same code with pointers looks like this:
main()
Fl_Window *win(300, 300);
// creates the window, all further widgets are added to win
Fl_Button *btn(10, 10, 100, 20);
// is atumatically added to win
win->end();
win->show();
Fl::run();
delete win;
// - win destroys all its children
// - btn removes itself from win and is destroyed
// - win is destroyed
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk