Hi Patrick,

I'll interleave some responses below, but I'm in a hurry so apologies in 
advance if I seem glib or harsh.

However, this is pretty basic stuff, so you might do better reading up on some 
programming courses or etc. online here...


On 10 Nov 2012, at 14:32, Patrick wrote:

> Apologies if these questions are really more C++ then FLTK....
> 
> In the test folder of the source there is a program called inactive.cxx. 
> I will copy and past the full program at the end of the email.
> 
> There is this line:
> Fl_Group *the_group=(Fl_Group *)0;

This is just telling the compiler that there is going to be a pointer to a 
group called "the_group", and it is explicitly being set to zero here as no 
actual group has been created to attach to the pointer at this point...

Note that this line is in global scope for the whole file in which it exists


> 
> 
> and this one:
> static void cb_active(Fl_Button*, void*) {
>   the_group->activate();
> }


Which calls the activate() method of the actual group pointed to be the_group. 
Note that the code *assumes* that the_group has had an actual group assigned to 
it by this point (which must be true if you consider the code flow to reach 
that point.)

If you were being *really cautious* you could do:

   if(the_group) the_group->activate();

instead, i.e. *only* try to dereference the_group if it is explicitly not NULL.

> 
> The object "the_group" is also instantiated here:
> the_group = new Fl_Group(25, 25, 375, 295, "activate()/deactivate() 
> called on this Fl_Group");

I think you misunderstand what this line is doing; it is creating a new group, 
then storing a pointer to it into the same "the_group" that was created and 
used above. This is not making a different "the_group" in the local scope, it 
is assigning something to the existing global one... if that makes sense...



> 
> I don't fully understand what is happening here. It looks like they are 
> both pointers to a group that will become deactivated by the callback 
> initiated by clicking on another button, they look to be in different 
> scopes.


There is only one "the_group" and its scope is global for the file in which it 
was declared, since it is declared at the outer level.
The thing you mistake for a redeclaration at local scope is only using the 
global pointer, it is not redeclaring a new version of "the_group" with a 
different scope.


> 
> Is the global one a pointer to a group or all of the groups instantiated 
> by the Fl_Group constructor? I am really mixed up.

Which is why you need to read some C / C++ books I guess.


> 
> In my own code I tried to make one button deactivate a spinner. I tried 
> to create a global pointer to the one that will be deactivated:
> Fl_Spinner *fc2_num_tubes=(Fl_Spinner *)0;


You have explicitly set fc2_num_tubes as a NULL pointer, so any attempt to 
dereference it will cause a segfault. You need to assign an actual spinner to 
that pointer before you can use it for anything.


> I would also like to double check that I understand what is happening here:
> 
> 
>   { Fl_Check_Button* o = new Fl_Check_Button(50, 170, 105, 25, "red");
>           o->type(102);
>           o->down_box(FL_DIAMOND_DOWN_BOX);
>           o->selection_color((Fl_Color)1);
>           o->labelcolor((Fl_Color)1);
>         } // Fl_Check_Button* o
> 
> 
>         { Fl_Check_Button* o = new Fl_Check_Button(50, 190, 105, 25, 
> "green");
>           o->type(102);
>           o->down_box(FL_DIAMOND_DOWN_BOX);
>           o->selection_color((Fl_Color)2);
>           o->labelcolor((Fl_Color)2);
>         } // Fl_Check_Button* o
> 
> By enclosing each snippet in curly braces, each instantiation is created 
> in it's own scope right ? and each instance can can access callbacks 
> defined in it's parental scope right?

Yeah, pretty much.



_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to