3

How do I get a class object of a certain class in GObject / Gtk? For example, if my class is GtkSpinButton, I want to get the instance of GtkSpinButtonClass that represents the class. It is the parameter "class" in

gtk_spin_button_class_init (GtkSpinButtonClass *class)

and it is the object where virtual functions are stored. When I have an instance of GtkSpinButton, I can call

GtkSpinButtonClass *class = GTK_SPIN_BUTTON_GET_CLASS (instance)

however I don't have an instance around. GTK_TYPE_SPIN_BUTTON gives me the type id, a number, and not the class object. Any idea how to get the actual instance?

jdm
  • 9,470
  • 12
  • 58
  • 110

1 Answers1

6

You want to use g_type_class_ref

GtkSpinButtonClass *klass = g_type_class_ref(GTK_TYPE_SPIN_BUTTON);

and when you're done with it

g_type_class_unref(klass);
grim
  • 760
  • 4
  • 13
  • 2
    Note that (A) `g_type_class_ref()` creates the class if needed, i.e. if no one else already did, and (B) there is `g_type_class_peek()` that avoids the need to create/ref/unref if you are sure the class already exists (or are happy to check for `NULL`) and do not need to hold a reference. See the full details at [the relevant area of the docs](https://developer.gnome.org/gobject/stable/gobject-Type-Information.html#g-type-class-ref). – underscore_d Apr 24 '18 at 20:28