Re: GObject Interface vs Pure Virtual Class



On Mon, 2007-05-14 at 12:12 -0700, Ana wrote:
> On Sat, May 12, 2007 at 02:42:50PM +0800, Kuang-Chun Cheng wrote:
> >    On 5/12/07, Jean Br*fort <[1]jean brefort normalesup org> wrote:
> ....
> > 
> >      A GObject class can inherit from only one parent (which might be
> >      virtual), but you can add as many Interfaces as you like. It is somewhat
> >      similar to multiple inheritance in C++ (more like the D language
> >      actually).
> > 
> >    Hi Jean,
> > 
> >    That's new to me ... I did not catch up this from GObject Ref. Manual ...
> >    Thanks.
> > 
> >    KC
> 
> It's also important to note that GTK+ Interfaces cannot directly
> implement any functionality, unlike C++ classes.  They only specify the
> set of (rigid) "prototypes" that *must* be fully implemented by the
> implementing class.

this is not entirely true. since a GTypeInterface is really just a
vtable of function pointers (plus the ability to have a set of
properties that can be overridden and all the stuff that comes from
being a type known to the GType system), it's not necessary for classes
implementing the interface to implement each and every function - as
long as the calls to the interface functions use a check like this one:

  if (MY_INTERFACE_GET_IFACE (instance)->some_method)
    MY_INTERFACE_GET_IFACE (instance)->some_method (instance, foo);
  else
    {
      warn_abort_or_do_nothing_at_all ();
    }

which allows you to have mandatory and optional methods.

there's also the possibility to actually have an implementation inside a
GTypeInterface itself, unlike in Java:

  if (MY_INTERFACE_GET_IFACE (instance)->another_method)
    return MY_INTERFACE_GET_IFACE (instance)->another_method (instance);
  else
    {
      gboolean bar;
      gint foo;
      g_object_get (instance, "bar", &bar, NULL);
      foo = foobarize (bar);
    }

this makes GTypeInterface more like a mixin or a role, in OOP.

ciao,
 Emmanuele.

-- 
Emmanuele Bassi,  E: ebassi gmail com
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net




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