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 Rob McDonald Sep 16, 2022  
 
On Friday, September 16, 2022 at 2:58:54 PM UTC-7 
Thx for the reply (sorry can't see your name).

As Greg Ercolano alluded, these differences have nothing to do with FLTK, but they are inherent to C++.  You could make the same choice for any object or variable in C++.

On this list, top-posting is frowned upon.

Google Groups is not great at keeping people's names attached to their posts.  For better or worse, there are about four kind souls who answer almost all of the questions on this list (I am not one of those people.  My role here is to ask questions.).  So, if you hang around for a little while, you'll quickly figure out who the players are and where their individual expertise and interest falls.

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);


The first is a 'local' instance.  It exists within the scope of the {} brackets it was created in.  It will automatically be destroyed when you cross the closing bracket}.

The second is a pointer to an instance.  It will exist until you use the delete operator on the pointer or the program terminates.  The pointer itself is an integer type (size depending on your machine architecture) that refers to the memory address for the object.

fltkButton.labelsize(12);  // fltkButton is an instance of class Fl_Button.  You access method of an instance with a . operator

fluidButton->labelsize(12); // fluidButton is a pointer to an instance of class Fl_Button.  fluidButton is an integer type that contains a memory address.  You dereference the memory address and then access a method of the instance with a -> operator

Typically, if you use the assignment operator '=' with an instance of an instance, a deep copy of that instance is made.  That can require allocating significant memory.  The same thing happens if you pass that instance to a function/method, or return an instance from a function / method.  Since you are passing by copy, you have pass by value semantics.

When you use the assignment operator '=' with a pointer to an instance, a copy of the pointer is made -- of course that copy points to the same memory address.  That allocation is cheap / simple because the pointer is a single integer value.  Passing a pointer to a function / method, and returning a pointer all are very cheap because you are passing around a single memory address.  However, all of this has pass by reference semantics -- any operations performed in any context can potentially change single instance of the variable pointed to.  Likewise, if one of the routines 'delete's the pointer, the instance is destroyed and all of the pointers become invalid.

Again -- none of this is FLTK.  This is all C++ (and most of it is common to (or at least very familiar to) pure C).

Rob






 

--
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/84d01f6f-3af8-45bc-ac45-a575fdd4cd68n%40googlegroups.com.
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'.