]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - untracked/docs/manual/html/sect-using-mem-func.html
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / untracked / docs / manual / html / sect-using-mem-func.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>Using a member function</title>
5 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
6 <link rel="home" href="index.html" title="libsigc++">
7 <link rel="up" href="chapter-connecting.html" title="Chapter 2. Connecting your code to signals">
8 <link rel="prev" href="chapter-connecting.html" title="Chapter 2. Connecting your code to signals">
9 <link rel="next" href="sect-signals-with-pars.html" title="Signals with parameters">
10 </head>
11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
12 <div class="navheader">
13 <table width="100%" summary="Navigation header">
14 <tr><th colspan="3" align="center">Using a member function</th></tr>
15 <tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="chapter-connecting.html">Prev</a> </td>
18 <th width="60%" align="center">Chapter 2. Connecting your code to signals</th>
19 <td width="20%" align="right"> <a accesskey="n" href="sect-signals-with-pars.html">Next</a>
20 </td>
21 </tr>
22 </table>
23 <hr>
24 </div>
25 <div class="section">
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="sect-using-mem-func"></a>Using a member function</h2></div></div></div>
28
29
30         <p>Suppose you found a more sophisticated alien alerter class on the web,
31         such as this:</p>
32
33 <pre class="programlisting">
34 class AlienAlerter : public sigc::trackable
35 {
36 public:
37     AlienAlerter(char const* servername);
38     void alert();
39 private:
40     // ...
41 };
42 </pre>
43
44         <p>(Handily it derives from <code class="literal">sigc::trackable</code> already. This isn't quite so
45         unlikely as you might think; all appropriate bits of the popular gtkmm library do so,
46         for example.)</p>
47
48         <p>You could rewrite your code as follows:</p>
49
50 <pre class="programlisting">
51 int main()
52 {
53     AlienDetector mydetector;
54     AlienAlerter  myalerter("localhost");       // added
55     mydetector.signal_detected.connect( sigc::mem_fun(myalerter, &amp;AlienAlerter::alert) ); // changed
56
57     mydetector.run();
58
59     return 0;
60 }
61 </pre>
62
63         <p>Note that only 2 lines are different - one to create an instance of the
64         class, and the line to connect the method to the signal.</p>
65
66         <p>This code is in example2.cc, which can be compiled in the same way as
67         example1.cc</p>
68
69         <p>It's possible to use a lambda expression instead of sigc::mem_fun(),
70         but it's not recommended, if the class derives from <code class="literal">sigc::trackable</code>.
71         With a lambda expression you would lose the automatic disconnection that the
72         combination of <code class="literal">sigc::trackable</code> and sigc::mem_fun()
73         offers.</p>
74 </div>
75 <div class="navfooter">
76 <hr>
77 <table width="100%" summary="Navigation footer">
78 <tr>
79 <td width="40%" align="left">
80 <a accesskey="p" href="chapter-connecting.html">Prev</a> </td>
81 <td width="20%" align="center"><a accesskey="u" href="chapter-connecting.html">Up</a></td>
82 <td width="40%" align="right"> <a accesskey="n" href="sect-signals-with-pars.html">Next</a>
83 </td>
84 </tr>
85 <tr>
86 <td width="40%" align="left" valign="top">Chapter 2. Connecting your code to signals </td>
87 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
88 <td width="40%" align="right" valign="top"> Signals with parameters</td>
89 </tr>
90 </table>
91 </div>
92 </body>
93 </html>