Re: Consensus on getter conventions?



> 
> 	It seems to me that we would break less existing code, and be more
> like existing conventions (most notably JavaBeans) if we used *_get_* to
> get references and something else for newly-allocated references (maybe
> *_copy_*, since we're getting a newly allocated copy of what's in object)

Just a guess in Java returning a reference doesn't mean that
it is needing to be freed (especially since Java has GC), but
the distinction is between a "get" of a field and a "accessor"
of the field.  An accessor would be one where changing the
return value of the function changes the object where it came from.

>From C++:
  class Foo {
    string s;
    public:
      string get_string() { return s; }
      string& access_string() { return s;}
  } f;  

  f.access_string()="hello";  // this changes f.


> 	Question: Does Qt have a convention for this?  What about Motif?
> MS-Windows/Mac?

There is really no need for such a convention in C++ because if
a C++ class returns a string (or QString) that class will be sure
to clean up the memory.  It also does the soft referencing so 
whether it is a copy or a new memory doesn't matter, because it
wont copy until you attempt to change the string.

Thus don't look to C++ kits for answers.   If fact isn't the concept
of returning something which needs to be freed going to be a C concept
only.  If you change the name of the function to show this convention
then the bindings are going to end up with rather pointless name
distinctions.  Why make that distinction for the bindings?  (and
using multiple names is going to cause the distinction to be
made in the bindings.)


My personal preference is to have all get_* functions which return
something be a pointer to a "static" piece of memory.  Those
cases where deallocation is required should return by argument.

  /* this doesn't need to be freed because it was the return */
  const gchar* gtk_widget_get_name(GtkWidget*);

  /* this one does need to be free and it is different for that reason */
  void gtk_font_selection_dialog_get_font_name(
        GtkFontSelectionDialog *fsd,  gchar**);
                                      ^^^^^
              notice it isn't const because we give it to the user.

This only creates a conflict for the functions which return multiple
values which you don't need to free.  (don't know if it helps the 
problem.)

--Karl






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]