gpt4 book ai didi

c - 在 glib 的 GObject 中,我混淆了从父类派生的子类的实例对象是否继承了父类的属性?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:15 25 4
gpt4 key购买 nike

第一期:
在GObject中我混淆了从父类派生的子类的实例对象是否继承了父类的属性?
第二期:
在 GObject 中,g_object_class_install_properties 函数在类初始化函数中将属性添加到自身类中,但实际上这些属性对于类的每个实例对象都有一个副本。换句话说,类的每个实例对象都有这些属性的副本。

此外,我阅读了 GObject 代码片段。Gobject.c文件中的如下代码:

class->set_property = g_object_do_set_property;
class->get_property = g_object_do_get_property;

首先上面的函数是什么时候调用的?
其次 如果子类派生自父类子类覆盖了这些方法(set_property 和 get_property),那么如果 g_object_new 创建新的子类实例并设置属性值,则仅调用 set_property 回调函数,调用子类set_property后是否调用父类的set_property方法?
不知道一次只调用子类的set_property后,是不是一次调用父类的set_property方法?

如果你知道这些问题,请花时间回答我的问题,非常感谢。

最佳答案

如果您还没有看到 GNOME Developer 网站,它有几页与您提出的问题相关的有用信息。下面指向的链接包含非常简单的示例代码,后面是对代码中发生的事情的非常详细的描述。我在下面引用(和链接)的示例页面专门解决了您的问题,但周围页面中提供了更多关于该主题的内容。

第一期:

Derivable types can be subclassed further, and their class and instance structures form part of the public API which must not be changed if API stability is cared about. They are declared using G_DECLARE_DERIVABLE_TYPE:

请看这里的例子: G_DECLARE_DERIVABLE_TYPE()

第二期:

generic get/set mechanism for object properties. When an object is instantiated, the object's class_init handler should be used to register the object's properties with g_object_class_install_properties.

请在此处查看示例: Object properties

我相信你的具体问题:
什么时候调用上面的函数?
在上面的 Object Properties 链接中解决了这些和周围段落的非常详细的问题:

If the user's GValue had been set to a valid value, g_object_set_property would have proceeded with calling the object's set_property class method. Here, since our implementation of Foo did override this method, execution would jump to foo_set_property after having retrieved from the GParamSpec the param_id [4] which had been stored by g_object_class_install_property.

Once the property has been set by the object's set_property class method, execution returns to g_object_set_property which makes sure that the "notify" signal is emitted on the object's instance with the changed property as parameter unless notifications were frozen by g_object_freeze_notify.

关于c - 在 glib 的 GObject 中,我混淆了从父类派生的子类的实例对象是否继承了父类的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com