df13a8dd82c84c94b082aad34c91420005b8cbc1
[platform/upstream/glib2.0.git] / docs / reference / gobject / html / howto-gobject-construction.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Object Construction</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
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-code.html" title="Boilerplate code">
10 <link rel="next" href="howto-gobject-destruction.html" title="Object Destruction">
11 <meta name="generator" content="GTK-Doc V1.13 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 <link rel="preface" href="pr01.html" title="Introduction">
14 <link rel="part" href="pt01.html" title="Part I. Concepts">
15 <link rel="chapter" href="chapter-intro.html" title="Background">
16 <link rel="chapter" href="chapter-gtype.html" title="The GLib Dynamic Type System">
17 <link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
18 <link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
19 <link rel="reference" href="rn01.html" title="API Reference">
20 <link rel="reference" href="rn02.html" title="Tools Reference">
21 <link rel="part" href="pt02.html" title="Part IV. Tutorial">
22 <link rel="chapter" href="howto-gobject.html" title="How to define and implement a new GObject">
23 <link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
24 <link rel="chapter" href="howto-signals.html" title="How to create and use signals">
25 <link rel="part" href="pt03.html" title="Part V. Related Tools">
26 <link rel="chapter" href="tools-vala.html" title="Vala">
27 <link rel="chapter" href="tools-gob.html" title="GObject builder">
28 <link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
29 <link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
30 <link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
31 <link rel="index" href="api-index-full.html" title="Index">
32 <link rel="index" href="api-index-deprecated.html" title="Index of deprecated symbols">
33 <link rel="index" href="api-index-2-2.html" title="Index of new symbols in 2.2">
34 <link rel="index" href="api-index-2-4.html" title="Index of new symbols in 2.4">
35 <link rel="index" href="api-index-2-6.html" title="Index of new symbols in 2.6">
36 <link rel="index" href="api-index-2-8.html" title="Index of new symbols in 2.8">
37 <link rel="index" href="api-index-2-10.html" title="Index of new symbols in 2.10">
38 <link rel="index" href="api-index-2-12.html" title="Index of new symbols in 2.12">
39 <link rel="index" href="api-index-2-14.html" title="Index of new symbols in 2.14">
40 <link rel="index" href="api-index-2-18.html" title="Index of new symbols in 2.18">
41 <link rel="index" href="api-index-2-22.html" title="Index of new symbols in 2.22">
42 <link rel="index" href="api-index-2-24.html" title="Index of new symbols in 2.24">
43 </head>
44 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
45 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
46 <td><a accesskey="p" href="howto-gobject-code.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
47 <td><a accesskey="u" href="howto-gobject.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
48 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
49 <th width="100%" align="center">GObject Reference Manual</th>
50 <td><a accesskey="n" href="howto-gobject-destruction.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
51 </tr></table>
52 <div class="sect1" title="Object Construction">
53 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
54 <a name="howto-gobject-construction"></a>Object Construction</h2></div></div></div>
55 <p>
56       People often get confused when trying to construct their GObjects because of the
57       sheer number of different ways to hook into the objects's construction process: it is
58       difficult to figure which is the <span class="emphasis"><em>correct</em></span>, recommended way.
59     </p>
60 <p>
61       <a class="xref" href="chapter-gobject.html#gobject-construction-table" title="Table 4. g_object_new">Table 4, “g_object_new”</a> shows what user-provided functions
62       are invoked during object instantiation and in which order they are invoked.
63       A user looking for the equivalent of the simple C++ constructor function should use
64       the instance_init method. It will be invoked after all the parent's instance_init
65       functions have been invoked. It cannot take arbitrary construction parameters 
66       (as in C++) but if your object needs arbitrary parameters to complete initialization,
67       you can use construction properties.
68     </p>
69 <p>
70       Construction properties will be set only after all instance_init functions have run.
71       No object reference will be returned to the client of <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-new" title="g_object_new ()">g_object_new</a></code>
72       until all the construction properties have been set.
73     </p>
74 <p>
75       As such, I would recommend writing the following code first:
76 </p>
77 <pre class="programlisting">
78 static void
79 maman_bar_init (MamanBar *self)
80 {
81   self-&gt;priv = MAMAN_BAR_GET_PRIVATE (self); 
82
83   /* initialize all public and private members to reasonable default values. */
84
85   /* If you need specific construction properties to complete initialization,
86    * delay initialization completion until the property is set. 
87    */
88 }
89 </pre>
90 <p>
91     </p>
92 <p>
93       Now, if you need special construction properties, install the properties in the class_init function,
94       override the set and get methods and implement the get and set methods as described in 
95       <a class="xref" href="gobject-properties.html" title="Object properties">the section called “Object properties”</a>. Make sure that these properties use a construct only 
96       <span class="type"><a class="link" href="gobject-GParamSpec.html#GParamSpec" title="GParamSpec">GParamSpec</a></span> by setting the param spec's flag field to G_PARAM_CONSTRUCT_ONLY: this helps
97       GType ensure that these properties are not set again later by malicious user code.
98 </p>
99 <pre class="programlisting">
100 static void
101 bar_class_init (MamanBarClass *klass)
102 {
103   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
104   GParamSpec *maman_param_spec;
105
106   gobject_class-&gt;set_property = bar_set_property;
107   gobject_class-&gt;get_property = bar_get_property;
108
109   maman_param_spec = g_param_spec_string ("maman",
110                                           "Maman construct prop",
111                                           "Set maman's name",
112                                           "no-name-set" /* default value */,
113                                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
114   g_object_class_install_property (gobject_class,
115                                    PROP_MAMAN,
116                                    maman_param_spec);
117 }
118 </pre>
119 <p>
120       If you need this, make sure you can build and run code similar to the code shown above. Make sure
121       your construct properties can set correctly during construction, make sure you cannot set them 
122       afterwards and make sure that if your users do not call <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-new" title="g_object_new ()">g_object_new</a></code>
123       with the required construction properties, these will be initialized with the default values.
124     </p>
125 <p>
126       I consider good taste to halt program execution if a construction property is set its
127       default value. This allows you to catch client code which does not give a reasonable
128       value to the construction properties. Of course, you are free to disagree but you
129       should have a good reason to do so.
130     </p>
131 <p>
132       Some people sometimes need to construct their object but only after
133       the construction properties have been set. This is possible through
134       the use of the constructor class method as described in
135       <a class="xref" href="chapter-gobject.html#gobject-instantiation" title="Object instantiation">the section called “Object instantiation”</a> or, more simply, using
136       the constructed class method available since GLib 2.12.
137     </p>
138 </div>
139 <div class="footer">
140 <hr>
141           Generated by GTK-Doc V1.13</div>
142 </body>
143 </html>