Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLib - difference between class_init and init class methods

Tags:

c

glib

I'm a newbie with glib and I'm still struggling to understand the difference between my_class_name_class_init() methods and my_class_name_init() methods.

I get that the latter is kinda equivalent to a C++ constructor and that goes per-instance of the object created but I don't quite understand the purpose of those my_class_name_class_init() methods. By reading the documentation I think class_init() methods are somewhat similar to a static constructor valid for all instances but I'm still not sure I got this right.

What's the purpose of class_init() methods?

like image 943
Dean Avatar asked Mar 08 '26 14:03

Dean


1 Answers

class_init functions are executed once per class, before first instance is constructed - in that way they are similar to C# static constructors. In contrast, instance_init functions are called for every instance of object created and are responsible for initializing that instance.

Like static constructors, class_init are responsible for initializing any shared data all instances might need, but more importantly, in GObject they play vital role in setting up GObject object system. They are responsible for:

  • Setting up virtual function tables
  • Setting up GObject property system
  • Setting up signals
like image 158
el.pescado - нет войне Avatar answered Mar 10 '26 04:03

el.pescado - нет войне



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!