Re: [gob-list] Overriding constructors

From: George <jirka_at_5z.com>
Date: Sat, 1 Feb 2003 09:18:44 -0800

On Sat, Feb 01, 2003 at 05:05:17PM +0100, Guillermo Ontañón wrote:
> Hi,
>
> I've been looking through the man page for gob, and tried a few things
> and connot find the proper way to override a constructor method. The
> purpose of this is being able to call the parent's constructor from my
> constructor.
>
> GOB won't let me declare the "new" as override, so I guess that's not
> the way to do it.
>
> A more concrete example will make my question clearer:
>
> suppose I have class foo:
>
> class foo from G:Object {
> private int a;
> public foo *new (int a) {
> foo *self = GET_NEW;
> self->_priv->a = a;
> return self;
> }
> }
>
> Now I want to create a subclass, I would like to provide a constructor
> that lets the parent initialize its stuff too:
>
> class fooChild from foo {
> private int b;
> public fooChild *new (int a, int b) {
> fooChild *self = GET_NEW;
> self->_priv->b = b;
> /*
> * and now I do not know how to call the parent
> * foo_new (a); would make a whole new object,
> * but I want foo to get the "a" value
> */
> }
> }
>
> I'm probably missing something here. Any help would be appreciated.

Do this the way most gtk+/gnome objects do add a construct method that does
the construction. These are used purely for deriving. So this would work
as:

class foo from G:Object {
        private int a;
        public foo *new (int a) {
                foo *self = GET_NEW;
        
                construct (self, a);

                return self;
        }
        public void construct (self, int a) {
                foo *self = GET_NEW;
                self->_priv->a = a;
                return self;
        }
}

class fooChild from foo {
        private int b;
        public fooChild *new (int a, int b) {
                fooChild *self = GET_NEW;
                self->_priv->b = b;

                foo_construct (self, a);
        }
}

Though I suppose fooChild should also get a construct method to be nice and
clean design.

George

-- 
George <jirka_at_5z.com>
   The ability to quote is a serviceable substitute for wit.
                       -- W. Somerset Maugham
Received on Sat Feb 01 2003 - 12:18:51 CST

This archive was generated by hypermail 2.2.0 : Sun Apr 17 2011 - 21:05:02 CDT