Search code examples
ccompiler-warningsglibgobjectvirtual-functions

How to deal with "incompatible pointer type" when assigning virtual methods in a derived class?


I have the to GLib classes Foo and DerivedFoo.

The Foo class has a bar () method:

typedef struct _FooClass
{
  GObjectClass parent_class;

  void (*bar) (Foo *self);
} FooClass;

The DerivedFoo class derives from Foo and implements the bar () method:

void derived_foo_bar (DerivedFoo *self);

static void
derived_foo_class_init (DerivedFooClass *klass)
{
  FooClass *foo_class = FOO_CLASS (klass);
  // Compiler warning appears here
  foo_class->bar = derived_foo_bar;
}

The warning message is:

warning: assignment from incompatible pointer type

The pointers are not compatible, because the type of the self parameter is different (Foo * vs. DerivedFoo *).

Is this the right way to implement virtual methods in GObject?

If so, can/should I do something about the compiler warning?


Solution

  • You leave the function prototype to respect the virtual base class, and typecast it in your function using glib macros/functions.

    void derived_foo_bar (Foo *self);
    
    static void
    derived_foo_class_init (DerivedFooClass *klass)
    {
      FooClass *foo_class = FOO_CLASS (klass);
      // Compiler warning appears here
      foo_class->bar = derived_foo_bar;
    }
    
    void derived_foo_bar (Foo *_self)
    {
      DerivedFoo *self = DERIVED_FOO (self); /* or whatever you have named this macro, using the standard GLIB semantics */
     /* If self is not compatible with DerivedFoo, a warning will be issued from glib typecasting logic */
    }