Re: chaining OBJECT_new() functions?



On Sat, Feb 19, 2005 at 11:47:15PM +0000, Roger Leigh wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Ben Johnson <ben blarg net> writes:
> 
> > I made a class that defines several _new() functions, which take various
> > parameters.  I now want to subclass from this and keep my various _new()
> > functions with more or less the same parameters, and I want the behavior
> > to stay the same too.  I don't see anything in the documentation about
> > how to do it though.  anyone know?
> 
> What about a factory function which allows one to choose the specific
> derived type.  For example,
> 
> GtkGroupedToggle *
> gtk_grouped_toggle_new_from_widget (GType               group_toggle_type,
>                                     GtkGroupedToggle   *group)
>      GtkGroupedToggle *new_toggle = NULL;
> 
>      g_return_val_if_fail(g_type_is_a(group_toggle_type,
>                                       GTK_TYPE_GROUPED_TOGGLE) == TRUE), NULL);
> 
>      new_toggle = g_object_new (type, NULL);
>      if( new_toggle )
>          new_toggle = gtk_grouped_toggle_add_to_list (new_toggle, group);
> 
>      return GTK_WIDGET(new_toggle);
> }
> 
> You now don't need any _new() functions for derived types.

That's an interesting idea.  So my objects of derived types would be
created...  like this?:

w = gtk_grouped_toggle_new_from_widget (HPNT_TYPE_GROUPED_TOGGLE, NULL);

huh.  that's pretty cool.  The base type doesn't need to know anything
about derived types, but it supports them anyway and cuts down on
coding.  I might do that.  (do I have it right? ...)

#define HPNT_TYPE_GROUPED_TOGGLE (hpnt_grouped_toggle_get_type ())


> Also, isn't it bad practice to have arguments to a _new() function
> which are not construction parameters?  Wouldn't it be better to add
> to group manually, or have g_object_new in a method of the container
> so it can't exist independently?

I'm not sure what you mean by construction parameters.  I figure
anything that aids in construction can be a construction parameter if
you want it to be.  I have these _from_from_widget() constructors
because it simplifies the creation.  My constructors for this are
patterned after the GtkRadioButton class which the new one is very
similar to.  GtkRadioButton has a number of _new_from_widget*() funcs.

Thanks!

- Ben




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