GLib

编程入门 行业动态 更新时间:2024-10-28 10:35:39
GLib - class_init和init类方法之间的区别(GLib - difference between class_init and init class methods)

我是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

更多推荐

本文发布于:2023-07-31 22:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GLib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!