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">
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>
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>
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>
30 <p>Suppose you found a more sophisticated alien alerter class on the web,
33 <pre class="programlisting">
34 class AlienAlerter : public sigc::trackable
37 AlienAlerter(char const* servername);
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,
48 <p>You could rewrite your code as follows:</p>
50 <pre class="programlisting">
53 AlienDetector mydetector;
54 AlienAlerter myalerter("localhost"); // added
55 mydetector.signal_detected.connect( sigc::mem_fun(myalerter, &AlienAlerter::alert) ); // changed
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>
66 <p>This code is in example2.cc, which can be compiled in the same way as
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()
75 <div class="navfooter">
77 <table width="100%" summary="Navigation footer">
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>
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>