GObject: Passing superclass construction parameters from subclass constructors



Hi list,

I'm wondering whether it's possible to pass construction parameters to
a superclass constructor from a subclass constructior.  Knowing that
you're supposed to ``chain up to the superclass constructor, the
absolutely first thing you do'', I'm a little scared to tamper with
the `construct_properties' before chaining up.

Also, I'm unsure as to what should happen if the user specifies their
own value for some of the parameters that I want to pass
automatically.  Am I ``overriding'' the parameters, or am I just
defining ``intelligent defaults'' for them?

C++ and Java, for example -- and in fact any other object-oriented
languages I can think of -- do not have this problem.  In those
languages, creating a subclass can only be done through the use of a
well-defined subclass constructor interface.

  class Parent {
    Resource resource;
    Parent(int size) {
      resource = new Resource(size);
    }
  }

  class Child extends Parent {
    private static final int UNIT_SIZE = 42;
    Child(int n_units) {
      super(n_units * UNIT_SIZE);
    }
  }

With this API, you can't create a Child while also specifying the
construction parameter of Parent.  The only way to create a Child is
by doing this:

  Child child = new Child(123);

With GObject, you could do this:

  Child *child = g_object_new (TYPE_CHILD,
                               "n-units", 123,
                               "size", 456,
                               NULL);

Any thoughts?


Regards,

-- 
Daniel Brockman
drlion deepwood net




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