Frage

I am new to GObject.I confused something about getting pointer to father of one class.For example , On Gobject Spec, we have a class:

struct _MamanBarClass {
  GObjectClass parent;

  /* class members */
};

what is the difference between :

MamanBarClass klass;
GObjectClass parent_class=G_OBJECT_CLASS(g_type_class_peek_parent (klass));

and

MamanBarClass klass;
GObjectClass g_object_class=G_OBJECT_CLASS(klass);

what is the difference between g_object_class and parent_class

One more question: the difference between casting MamanBarClass(klass) and MAMANBARCLASS(klass) thank you!

War es hilfreich?

Lösung

In GObject, any new type keeps a class struct in a register to keep runtime info, virtual methods and private class data.

The casting (G_OBJECT_CLASS(klass)) is used to retrieve the GObjectClass of klass while G_OBJECT_CLASS(g_type_class_peek_parent(klass)) returns the GObjectClass of the parent of klass. In your example, the former simply returns the GObjectClass struct of MamanBarClass while the latter returns the GObjectClass struct of the GObjectClass struct (the parent of MamanBarClass) found in the register. In practice:

klass = G_OBJECT_CLASS(maman_bar_class);
/* This will override MamanBar::dispose */
klass->dispose = my_dispose;
/* Now my_dispose will be called by g_object_dispose(object) when object is a MamanBar object */

klass = G_OBJECT_CLASS(g_type_class_peek_parent(klass));
/* This will override GObject::dispose */
klass->dispose = my_dispose
/* Now my_dispose will be called by g_object_dispose(object) when object is a GObject (the parent class of MamanBar) object */

(MamanBarClass *) klass and MAMAN_BAR_CLASS(klass) are equivalent, but the latter performs runtime type checking (emits a g_critical if klass is not a MamanBarClass *).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top