Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / docs / manual / html / ch02s03.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Signals with parameters</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="ch02s02.html" title="Using a member function"><link rel="next" href="ch02s04.html" title="Disconnecting"></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">Signals with parameters</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s02.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="ch02s04.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idm45801113077280"></a>Signals with parameters</h2></div></div></div><p>Functions taking no parameters and returning void are quite useful,
2         especially when they're members of classes that can store unlimited amounts of
3         safely typed data, but they're not sufficient for everything.</p><p>What if aliens don't land in the carpark, but somewhere else? Let's modify
4         the example so that the callback function takes a <code class="literal">std::string</code> with the location
5         in which aliens were detected.</p><p>I change my class to:</p><pre class="programlisting">
6 class AlienDetector
7 {
8 public:
9     AlienDetector();
10
11     void run();
12
13     sigc::signal&lt;void, std::string&gt; signal_detected;      // changed
14 };
15 </pre><p>The only line I had to change was the signal line (in <code class="literal">run()</code> I need to change
16         my code to supply the argument when I emit the signal too, but that's not shown
17         here).</p><p>The name of the type is '<code class="literal">sigc::signal</code>'. The template parameters are the return type, then the argument types.</p><p>The types in the function signature are in the same order as the template
18         parameters, eg:</p><pre class="programlisting">
19 sigc::signal&lt;void,         std::string&gt;
20               void function(std::string foo);
21 </pre><p>So now you can update your alerter (for simplicity, lets go back to the
22                 free-standing function version):</p><pre class="programlisting">
23 void warn_people(std::string where)
24 {
25     cout &lt;&lt; "There are aliens in " &lt;&lt; where &lt;&lt; "!" &lt;&lt; endl;
26 }
27
28 int main()
29 {
30     AlienDetector mydetector;
31     mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
32
33     mydetector.run();
34
35     return 0;
36 }
37 </pre><p>Easy.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s02.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="ch02s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Using a member function </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Disconnecting</td></tr></table></div></body></html>