Re: when should I create child objects?





2010/4/16 Xi Yang <jiandingzhe msn com>
I'm attempting to build a Goo::Canvas::Group subclass. It has a 'displayName' property, and would show the displayName using a Goo::Canvas::Text object.

###### in CanvasExpand.pm ######
package CanvasExpand;

use Glib::Object::Subclass('Goo::Canvas::Group',
ÂÂÂ properties => [
ÂÂÂ ÂÂÂ Glib::ParamSpec->string(
ÂÂÂ ÂÂÂ ÂÂÂ 'displayName','','',
ÂÂÂ ÂÂÂ ÂÂÂ 'default_value',
ÂÂÂ ÂÂÂ ÂÂÂ [qw/readable writable/]
ÂÂÂ ÂÂÂ )
ÂÂÂ ],
);

sub INIT_INSTANCE {
ÂÂÂ my $self = shift;
ÂÂÂ my $name = $self->get('displayName'); # here gots 'default_value'
ÂÂÂ Goo::Canvas::Text->new($self,$name,0,0,-1,'center');
}

##################

However, while creating the Text object in INIT_INSTANCE function, the displayName property remains to be uninitialized: the default value is retrieved, rather than the value provided in constructor. So, when should I build all children?

Good question!

What I do is that I provide my own new and call the parent's new first. I then build my properties there.

sub new {
 my $class = shift;
 my $self = $class->SUPER::new(@_);
 $self->set_XXX($xxx);
 return $self;
}

I'm wondering if there's a better way too.

--
Emmanuel Rodriguez


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