8
8
<link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject">
9
9
<link rel="prev" href="howto-gobject-methods.html" title="Object methods">
10
10
<link rel="next" href="howto-interface.html" title="How to define and implement interfaces">
11
<meta name="generator" content="GTK-Doc V1.18.1 (XML mode)">
11
<meta name="generator" content="GTK-Doc V1.19 (XML mode)">
12
12
<link rel="stylesheet" href="style.css" type="text/css">
14
14
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
32
32
<li class="listitem"><p>In the method B::foo, the child class B calls its parent class method A::foo.</p></li>
35
There are many uses to this idiom:
35
There are various uses to this idiom:
37
37
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
38
<li class="listitem"><p>You need to change the behaviour of a class without modifying its code. You create
38
<li class="listitem"><p>You need to extend the behaviour of a class without modifying its code. You create
39
39
a subclass to inherit its implementation, re-implement a public virtual method to modify the behaviour
40
slightly and chain up to ensure that the previous behaviour is not really modified, just extended.
40
and chain up to ensure that the previous behaviour is not really modified, just extended.
42
<li class="listitem"><p>You are lazy, you have access to the source code of the parent class but you don't want
43
to modify it to add method calls to new specialized method calls: it is faster to hack the child class
44
to chain up than to modify the parent to call down.</p></li>
45
42
<li class="listitem"><p>You need to implement the Chain Of Responsibility pattern: each object of the inheritance
46
43
tree chains up to its parent (typically, at the beginning or the end of the method) to ensure that
47
44
they each handler is run in turn.</p></li>
50
I am personally not really convinced any of the last two uses are really a good idea but since this
51
programming idiom is often used, this section attempts to explain how to implement it.
54
49
To explicitly chain up to the implementation of the virtual method in the parent class,
56
51
access the original class function pointer and invoke it directly.
57
52
<a href="#ftn.id-1.6.3.8.3.1" class="footnote" name="id-1.6.3.8.3.1"><sup class="footnote">[11]</sup></a>
59
<p>The function <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-class-peek-parent" title="g_type_class_peek_parent ()">g_type_class_peek_parent</a></code> is used to access the original parent
60
class structure. Its input is a pointer to the class of the derived object and it returns a pointer
61
to the original parent class structure. The code below shows how you could use it:
54
<p>The function <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-class-peek-parent" title="g_type_class_peek_parent ()">g_type_class_peek_parent</a></code>
55
is used to access the original parent class structure. Its input is a
56
pointer to the class of the derived object and it returns a pointer to
57
the original parent class structure. Instead of using this function
58
directly, though, you should use the <code class="function">parent_class</code>
59
pointer created and initialized for us by the G_DEFINE_TYPE_* family of
63
62
<pre class="programlisting">
65
64
b_method_to_call (B *obj, int a)
70
klass = B_GET_CLASS (obj);
71
parent_class = g_type_class_peek_parent (klass);
73
66
/* do stuff before chain up */
75
parent_class->method_to_call (obj, a);
68
/* call the method_to_call() virtual function on the
69
* parent of BClass, AClass.
71
* remember the explicit cast to AClass*
73
A_CLASS (b_parent_class)->method_to_call (obj, a);
77
75
/* do stuff after chain up */