Re: any policy for foo_new() return types?



Mark Leisher <mleisher crl nmsu edu> writes:

>     Stefan> I believe that it's common policy to return a GtkWidget* if this
>     Stefan> is available in the ancestry, and otherwise fall back to a
>     Stefan> GtkObject -- but why this policy, anyway? And why do some of the
>     Stefan> above examples draw away from this policy?
>
> I really like GTK (but don't use GNOME).  After many years of using
> the Intrinsics, I got used to object creation calls all returning
> Widget*, and all widget-related functions taking Widget* arguments,
> leaving the functions to check for valid subclasses instead of the
> programmer doing it explicitly.

ALthough I haven't talked to Peter about the issue, so I can't
tell you the _real_ reason for it, my take on the reasons why
having functions take the most derived type useful is:

 - It makes the header files clearer 

 - Combined with standard programmer habits, it provides some measure
   of compile time safety; because if I write:

    GtkWidget *box = gtk_hbox_new (FALSE, 0);
    GtkWidget *entry = gtk_entry_new ();
    gtk_container_add (GTK_ENTRY (entry), GTK_CONTAINER (box));

   The compiler will catch it. Of course, if I write  
   
    GtkWidget *box = gtk_hbox_new (FALSE, 0);
    GtkWidget *entry = gtk_entry_new ();
    gtk_container_add (GTK_CONTAINER (entry), GTK_ENTRY (box));

   It won't get caught until run time, but that's a harder mistake
   to make.

Regards,
                                        Owen




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