1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Chaining up</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject">
9 <link rel="prev" href="howto-gobject-methods.html" title="Object methods">
10 <link rel="next" href="howto-interface.html" title="How to define and implement interfaces">
11 <meta name="generator" content="GTK-Doc V1.18 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="howto-gobject-methods.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="howto-gobject.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GObject Reference Manual</th>
20 <td><a accesskey="n" href="howto-interface.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="howto-gobject-chainup"></a>Chaining up</h2></div></div></div>
25 <p>Chaining up is often loosely defined by the following set of
28 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
29 <li class="listitem"><p>Parent class A defines a public virtual method named <code class="function">foo</code> and
30 provides a default implementation.</p></li>
31 <li class="listitem"><p>Child class B re-implements method <code class="function">foo</code>.</p></li>
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:
37 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
38 <li class="listitem"><p>You need to change the behaviour of a class without modifying its code. You create
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.
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 <li class="listitem"><p>You need to implement the Chain Of Responsibility pattern: each object of the inheritance
46 tree chains up to its parent (typically, at the beginning or the end of the method) to ensure that
47 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 To explicitly chain up to the implementation of the virtual method in the parent class,
55 you first need a handle to the original parent class structure. This pointer can then be used to
56 access the original class function pointer and invoke it directly.
57 <sup>[<a name="idp43711952" href="#ftn.idp43711952" class="footnote">11</a>]</sup>
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:
63 <pre class="programlisting">
65 b_method_to_call (B *obj, int a)
70 klass = B_GET_CLASS (obj);
71 parent_class = g_type_class_peek_parent (klass);
73 /* do stuff before chain up */
75 parent_class->method_to_call (obj, a);
77 /* do stuff after chain up */
82 <div class="footnotes">
83 <br><hr width="100" align="left">
84 <div class="footnote"><p><sup>[<a id="ftn.idp43711952" href="#idp43711952" class="para">11</a>] </sup>
85 The <span class="emphasis"><em>original</em></span> adjective used in this sentence is not innocuous. To fully
86 understand its meaning, you need to recall how class structures are initialized: for each object type,
87 the class structure associated to this object is created by first copying the class structure of its
88 parent type (a simple <code class="function">memcpy</code>) and then by invoking the class_init callback on
89 the resulting class structure. Since the class_init callback is responsible for overwriting the class structure
90 with the user re-implementations of the class methods, we cannot merely use the modified copy of the parent class
91 structure stored in our derived instance. We want to get a copy of the class structure of an instance of the parent
98 Generated by GTK-Doc V1.18</div>