Re: Inheritance (Was:Non-technical Glib advise needed)



On Mon, 2004-01-12 at 09:53 -0700, Nix wrote:
On Sun, 2004-01-11 at 15:09, John (J5) Palmieri wrote:
[...]
GObjects supports inheritence so it might be a good solution to your
dependecy graph.

But only inheritance of the creation and destruction methods, right ?
There's no way to do polymorphism though, right ?  To illustrate:

Sure it can.

Moral of the story:  Can anybody show me a systematic way of doing
polymorphism using GObject ?

Your structures

struct GDrawingObjectClass {
        GObjectClass parent_class;

        void (* draw) (GDrawing *,
                        GdkGC *,
                        GdkDrawable *);
};

struct GCircleClass {
        GDrawingObjectClass parent_class;
};

then in your class init functions:

void
g_circle_class_init (GCircleClass *klass)
{
        GDrawingObjectClass *do_class = (GDrawingObjectClass*)klass;

        do_class->draw = g_circle_draw;
}

and back to the implementation of g_drawing_object_draw:

#define DO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DO_TYPE,
GDrawingObjectClass))

void
g_drawing_object_draw (GDrawingObject *obj,
                        GdkGC *gc,
                        GdkDrawable *drawable)
{
        GDrawingObjectClass *klass = DO_GET_CLASS (obj);

        klass->draw (obj, gc, drawable);
}

depending on the type that GDrawingObject is when you call it, it will
either call g_circle_draw or g_square_draw.

Hope that helps. You could look at gnome-media/gnome-cd/cdrom.[ch] for
more pointers.

iain
-- 




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