Re: GObject constructors



On Wed, Nov 09, 2005 at 06:43:17PM +0100, Jacob Kroon wrote:
> I'm having problems with figuring out how to initialize my data properly.
> My scenario is that I have:
> 
> struct Parent {
>    GObject parent;
>    float *data;
> }
> 
> struct ParentClass {
>    GObjectClass parent;
>    int data_size;
> }
> 
> struct Child {
>    Parent parent;
> }
> 
> struct ChildClass {
>    ParentClass parent;
> }
> 
> Each subclass of Parent will use different, but fixed data sizes, so I 
> want Child to tell Parent how much
> data it needs, and then let Parent initialize the data accordingly.
> 
> The way I thought would work was this:
> 
> 1. In child_class_init(*klass):
>    PARENT_CLASS(klass)->data_size = specific_size_for_this_subclass;
> 2. In parent_instance_init(*obj)
>    klass = PARENT_GET_CLASS(obj);
>    obj->data = g_new(float, klass->data_size);
> 
> but this wont work since PARENT_GET_CLASS(obj) in parent_instance_init() 
> returns the class for Parent,
> not the subclass ChildClass, so data_size won't have the value I intended.

I had similar problem and came to conclusion it's impossible
to implement it this way, or at least it's against inheritance.

However, you can keep 1. as it is and define a parent_instance_setup()
method that contains the common code and call that from
child constructor:

2a. parent_instance_setup(obj) {
        klass = PARENT_GET_CLASS(obj)
        obj->data = g_new(float, klass->data_size);
    }

2b. child_instance_init(obj) {
        parent_instance_setup(obj);
    }

Yeti


--
That's enough.



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