Re: any policy for foo_new() return types?



Stefan Rieken <StefanRieken SoftHome net> writes:

> Hello,
> 
> First off, I've been so rude to post to this list while not being on it.
> Please CC me, thanks.
> 
> I have used GNOME and GTK+ for a long time, but only in the past few
> days I really got past the "Hello, World" stage. I couldn't help
> noticing that every foo_new() method had its own policy for what kind of
> struct to return. A few examples (sorry for using GNOME examples):
> 
> - gnome_app_bar_new() returns a GtkWidget*
>   (its far, but not "oldest" ancestor)
> - gnome_mdi_new() returns a GtkObject*
>   (its "oldest", and only ancestor)
> - gnome_mdi_generic_child_new() returns a GnomeMDIGenericChild
>   (an instance of itself)
> - gtk_adjustment_new() returns a GtkObject*
>   (its "oldest", but not its only ancestor)
> 
> I believe that it's common policy to return a GtkWidget* if this is
> available in the ancestry, and otherwise fall back to a GtkObject -- but
> why this policy, anyway? And why do some of the above examples draw away
> from this policy?

The policy is basically that the constructor should return the
most derived types with a few exceptions:

 - Constructors of types derived from GtkWidget should return
   a GtkWidget.

   The basic reason why widget constructors return a GtkWidget is
   the next step after creation is almost always adding to a container. 

 - The same applies to other cases when the class is more likely
   to be used as a base class than as the derived class - for
   example GtkIMContext in GTK+-2.0.

   This concept is a little fuzzy, but works OK in practice.

GnomeMDI and GtkAdjustment are broken. GtkAdjustment is that way
for compatibility reasons, but unfortunately its been copied
as a bad example in a few other places.

GTK+-2.0 introduces a big swarm of non-widget GObject types,
(PangoLayout, GdkWindow, GdkGC, ....) so there will be lots
of good examples.

Regards,
                                        Owen





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