Re: chaining OBJECT_new() functions?

Hash: SHA1

Ben Johnson <ben blarg net> writes:

> On Sat, Feb 19, 2005 at 11:47:15PM +0000, Roger Leigh wrote:

>> 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? ...)

Yes, you've got it.  (This is what the GnomeCanvas does, all derived
GnomeCanvasItems are created with gnome_canvas_item_new().

> #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.

Sorry, I meant construction /properties/ (properties created with
G_PARAM_CONSTRUCT), which are passed to g_object_new() for object

My understanding (which may be wrong) is that doing anything else in a
new() function (rather than in the object_init(), object_construct()
or with a construction parameter) makes is difficult to write language


- -- 
Roger Leigh
                Printing on GNU/Linux?
                Debian GNU/Linux
                GPG Public Key: 0x25BFB848.  Please sign and encrypt your mail.
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <>


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