我是glib的新手,我仍然在努力理解my_class_name_class_init()方法和my_class_name_init()方法之间的区别。
我知道后者有点等同于C ++构造函数,它是创建的对象的每个实例,但我不太明白这些my_class_name_class_init()方法的目的。 通过阅读文档,我认为class_init()方法有点类似于对所有实例都有效的静态构造函数,但我仍然不确定我是否正确。
class_init()方法的目的是什么?
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?
最满意答案
class_init函数在构造第一个实例之前每个类执行一次 - 这样它们就类似于C#静态构造函数 。 相反,为每个创建的对象实例调用instance_init函数,并负责初始化该实例。
与静态构造函数一样, class_init负责初始化所有实例可能需要的任何共享数据,但更重要的是,在GObject中它们在设置GObject对象系统中起着至关重要的作用。 他们负责:
设置虚拟功能表 设置GObject属性系统 设置信号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更多推荐
发布评论