Re: GObject: NULL virtual methods during instance init



Gustavo J A M Carneiro <ee96090 fe up pt> writes:

>   I'm getting something weird. I have class A and class B, where class B
> is derived from class A. Class A defines a virtual method, "vfunc",
> which is pure virtual and only implemented in class B.
> I created an instance of class B. During class A's instance init func
> (but called with a B object) I try to invoke vfunc this way:
> 
>  AClass *class;
>  class = A_GET_CLASS(self);
>  if (class->vfunc)
>     class->vfunc(self, args);
> 
> But at this point class->vfunc is NULL. I do initialize it correctly at
> class B's class_init function.
>   Generally, I've been using GObject with virtual methods for some time
> and had no problem at all until now.
>   Am I doing anything wrong here? What?
>   Thanks in advance for any help.

In gtk_type_new() in GTK+-1.2 there is an explanation for this behavior.
I think it's a little weird, but hard to change now.

  /* we need to call the base classes' object_init_func for derived
   * objects with the object's ->klass field still pointing to the
   * corresponding base class, otherwise overridden class functions
   * could get called with partly-initialized objects. the real object
   * class is passed as second argment to the initializers.
   */
 
Regards,
                                        Owen




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