Re: Why do constructors return GtkWidget?
- From: Danielle Madeley <danielle madeley collabora co uk>
- To: Cody Russell <bratsche gnome org>
- Cc: gtk-devel-list <gtk-devel-list gnome org>
- Subject: Re: Why do constructors return GtkWidget?
- Date: Wed, 04 Nov 2009 17:06:54 +1100
On Tue, 2009-11-03 at 23:47 -0600, Cody Russell wrote:
> On Wed, 2009-11-04 at 06:23 +0100, Javier Jardón wrote:
> > The question: why is it normal for GTK widget "constructors" to return
> > GtkWidget and not their real type?
> >
> > For instance I would expect:
> >
> > gtk_menu_item_new ()
> >
> > To return GtkMenuItem*. But it doesn't, it returns GtkWidget*.
> > IMHO is much clear that constructors return their real type, instead
> > GtkWidget. But maybe there is a technical reason for this.
>
> Almost invariably you're going to be doing something with the return
> value as a GtkWidget, such as calling gtk_widget_show() on it or packing
> it into a container. If that method above returned GtkMenuItem* then
> that guarantees a cast, whereas if it returns a GtkWidget* then it may
> just get packed into a container and that's the end of it.
What Cody is trying to say is that the pointer type returned is a
convention for convenience, since C doesn't have the idea of type
inheritance.
If you were to call G_OBJECT_TYPE() it would return GTK_TYPE_MENU_ITEM.
--
Danielle Madeley
Collabora Ltd., Melbourne, Australia
http://www.collabora.co.uk/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]