您可以让函数原型尊重虚拟基类,并使用glib宏/函数在函数中对其进行类型转换。
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 */
}