Re: FLTK usage in passing pointers from file to file
Greg Ercolano
Feb 25, 2021
On 2/25/21 5:19 PM, John E. Kaye P.Eng.
wrote:
Thank you for your response and comment. I had declared the Window
pointers Globally such as Fl_Window *w1, Fl_Window *w2 etc BUT
defined them in the main() program in FILE 1 as Fl_Window
w1(5,198,600,275,"Tester"), etc
This is bad; sounds like you're declaring two different w1
variables; one global.
Functions outside main() will only see the uninitialized global
pointer variable 'w1',
not the initialized one inside main().
Let's ignore w2, and just focus on w1. Sounds like you have
this:
Fl_Window *w1; // OK: declares global pointer variable
w1 int main() { Fl_Window w1(...); //
BAD: declares a separate
local variable w1 that shadows the global one .. }
Other functions will only see the global 'w1' which is
uninitialized by your code,
and is causing the crashes.
What you should do instead is only declare 'w1' once, as a
global,
then set it in main() using the code in green:
// main.cxx Fl_Window *w1 = 0; // declare global pointer w1, init to
null
int main(..) { // Create window w1, saving pointer to
created window in w1 w1 = new
Fl_Window(5,198,600,275,"Tester");
// define widgets that go inside w1.. w1->end();
// From here, you can call your functions in your other
file // that use w1 and it should work
return Fl::run(); // application loop
}
That should work correctly.
Any further comments??
Yes, be careful of variable scoping in C and C++.
It's possible to have many variables that are completely
different, but all
have the same name, so be careful of that. I believe that's what
bit you here.
And when working with pointers to FLTK widgets, you want to
create the widgets
with this pattern:
Fl_Some_Widget *wa = new Fl_Some_Widget(...); Fl_Other_Widget *wb = new Fl_Other_Widget(...);
Note the use of 'new' to create an instance of the widget's
class, and saving its
value in the pointer.
Some widgets are containers, like windows and groups, so you
need to use
begin() and end() when creating widgets that go inside those
containers.
Comments are owned by the poster. All other content is copyright 1998-2025 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.