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



On Mon, 2004-01-12 at 11:56, Murray Cumming Comneon com 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 ?

Yes, but you need to implement the virtual function table yourself. See
GtkCellRenderer for instance.

This is one of the things I mentioned that other languages handle where
as using GObjects you need to write the boilerplate code yourself. In
the case of your GDrawObject example you would need to create a pointer
to a function in your class struct called draw:

void  (* draw)     (GDrawingObject *do, 
                    GdkGC *gc, 
                    GdkDrawable *gd);

You would then in the class constructors assign draw to g_circle_draw
and g_square_draw depending on the class:

GObjectClass *object_class = G_OBJECT_CLASS (class);
GDrawingObject *cell_class = G_DRAW_OBJECT_CLASS (class);
parent_class = g_type_class_peek_parent (class);

parent_class->draw = g_circle_draw;

You would then have method g_drawing_object_draw (...) that would access it's
class and call the draw function through the vtable:

G_DRAWING_OBJECT_GET_CLASS (self)->draw(...)

A bit of setup is involved but at least you get to see how OO realy works :-)
And it is not all that bad just a lot of typing. Look at the cell render code 
as Murray suggested.  It is very steight forward and easy to read code.

--
J5





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