FLTK logo

Re: [fltk.general] Syntax Variations (My original post seemed to disappear

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Syntax Variations (My original post seemed to disappear Albrecht Schlosser Sep 18, 2022  
 
On 9/17/22 01:25 Greg Ercolano wrote:
On 9/16/22 14:48, roger tunnicliffe wrote:
Thx for the reply (sorry can't see your name).

I am wondering then what would be the use cases for each particular syntactical version. ie.

When and why would i use this code....
        Fl_Button fltkButton(25, 20, 70, 50, "button1");
             noting it requires fltkButton.labelsize(12);
as opposed to this code....
        Fl_Button *fluidButton = new Fl_Button(25, 80, 70, 50, "button2"); 
            noting it requires fltkButton->labelsize(12);

    Probably no good reason, other than it's shorter.

There may be cases where you want a C++ object to be deleted when the current scope is left. One (mostly internal) FLTK example is Fl_Widget_Tracker which is usually allocated on the stack (a local variable) so it is automatically deleted at the end of the function or method.

    If you're new to C++, I'd suggest always using 'new' to create
    FLTK widgets so you don't have to worry about widgets going
    out of scope prematurely.

It should be noted that the pointer you use when allocating with `new` would also go out of scope eventually and thus it can't be used later unless saved as Kevin described in another reply.

An important point here is that the "automatic parenting mode" of FLTK usually adds a widget to the current group (window) so you can "forget" the pointer because you will likely not use it later. Working example code with comments below:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
int main(int argc, char **argv) {
// allocate window on stack: scope/lifetime is main()
Fl_Window window(240, 140, "main window");
{ // introduce a local block (scope)
// allocate button b with operator new
Fl_Button *b = new Fl_Button(20, 20, 200, 100, "Hello, world!");
// use the pointer b
b->color(FL_YELLOW);
// pointer b goes out of scope but the window keeps a pointer
// to the button and the widget will not (yet) be destroyed
}
window.end(); // end automatic parenting
window.show(argc, argv);
return Fl::run();
// the window 'window' will be deleted after main() and
// its destructor will also delete the button
}
You can build this example program with `fltk-config --compile demo.cxx`.

If you would allocate the button on the stack in the local scope rather than using `new` it would be deleted at the end of the block and it would be removed from the window at that time (try this).

I didn't bother to add a button callback, you need to close the window with the window's close ('X') button or hit the 'Esc' key to end the program.

I hope this helps.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/f05fb5e3-982d-5ca9-17e5-98d4ff81abd5%40online.de.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.