Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / docs / manual / html / ch02s02.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Using a member function</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="libsigc++"><link rel="up" href="ch02.html" title="Chapter 2. Connecting your code to signals"><link rel="prev" href="ch02.html" title="Chapter 2. Connecting your code to signals"><link rel="next" href="ch02s03.html" title="Signals with parameters"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Using a member function</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th width="60%" align="center">Chapter 2. Connecting your code to signals</th><td width="20%" align="right"> <a accesskey="n" href="ch02s03.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idm45801113083056"></a>Using a member function</h2></div></div></div><p>Suppose you found a more sophisticated alien alerter class on the web,
2         such as this:</p><pre class="programlisting">
3 class AlienAlerter : public sigc::trackable
4 {
5 public:
6     AlienAlerter(char const* servername);
7     void alert();
8 private:
9     // ...
10 };
11 </pre><p>(Handily it derives from <code class="literal">sigc::trackable</code> already. This isn't quite so
12         unlikely as you might think; all appropriate bits of the popular gtkmm library do so,
13         for example.)</p><p>You could rewrite your code as follows:</p><pre class="programlisting">
14 int main()
15 {
16     AlienDetector mydetector;
17     AlienAlerter  myalerter("localhost");       // added
18     mydetector.signal_detected.connect( sigc::mem_fun(myalerter, &amp;AlienAlerter::alert) ); // changed
19
20     mydetector.run();
21
22     return 0;
23 }
24 </pre><p>Note that only 2 lines are different - one to create an instance of the
25         class, and the line to connect the method to the signal.</p><p>This code is in example2.cc, which can be compiled in the same way as
26         example1.cc</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch02.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch02s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. Connecting your code to signals </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Signals with parameters</td></tr></table></div></body></html>