Re: macro IS_xxx



On Sun, 2007-06-24 at 11:34 +0200, Andrea Zagli wrote:
Il giorno dom, 24/06/2007 alle 10.32 +0200, David NeÄas (Yeti) ha
scritto:
On Sun, Jun 24, 2007 at 09:48:01AM +0200, Andrea Zagli wrote:
i have GObject A and GObject B; B is a A's subclass

obj_a = a_new ();
obj_b = b_new ();

(a_new() and b_new() return GObject)

when i call IS_OBJECT_A (obj_b) it returns TRUE: why?

Because obj_b *is* also an instance of A.  That's the
whole point of subclassing.

To check the exact type you can get it with
G_TYPE_FROM_INSTANCE and compare.  However, in most cases
needing this means there's something wrong with your design.

i need it because i pass obj_a/obj_b to a function that do different
things based on object type

is there a different way to make it without check the object type?

is it a bug? or is it normal?

Show me a single object system implementation that behaves
differently in this regard...

i don't know... but i'm not an expert of oop

If you are going to change behaviour by means of switch (or if/else)
blocks based on the dynamic type, then there is little point in
sub-classing type B from type A in the first place.

If you want to implement your behaviour via sub-classing (which would be
appropriate if there is an "is-a" relationship between your GObject
types A and B), then you probably want to implement a virtual method,
that is, a function call which is dispatched automatically by the
GObject system by reference to the dynamic type of the object on which
the call is made.  That is the "oop" way of doing it.  For this purpose,
see:

http://developer.gnome.org/doc/API/2.0/gobject/howto-gobject-methods.html#id2715711

Chris





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