3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>Signals with parameters</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="sect-using-mem-func.html" title="Using a member function">
9 <link rel="next" href="sect-disconnecting.html" title="Disconnecting">
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">Signals with parameters</th></tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="sect-using-mem-func.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-disconnecting.html">Next</a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="sect-signals-with-pars"></a>Signals with parameters</h2></div></div></div>
30 <p>Functions taking no parameters and returning void are quite useful,
31 especially when they're members of classes that can store unlimited amounts of
32 safely typed data, but they're not sufficient for everything.</p>
34 <p>What if aliens don't land in the carpark, but somewhere else? Let's modify
35 the example so that the callback function takes a <code class="literal">std::string</code> with the location
36 in which aliens were detected.</p>
38 <p>I change my class to:</p>
40 <pre class="programlisting">
48 sigc::signal<void(std::string)> signal_detected; // changed
52 <p>The only line I had to change was the signal line (in <code class="literal">run()</code> I need to change
53 my code to supply the argument when I emit the signal too, but that's not shown
56 <p>The name of the type is '<code class="literal">sigc::signal</code>'.
57 The template parameters are the return type, then the argument types in parentheses.
58 (libsigc++2 also accepts a different syntax, with a comma between the return type
59 and the parameter types. That syntax is deprecated, though.)</p>
61 <p>The types in the function signature are in the same order as the template
64 <pre class="programlisting">
65 sigc::signal<void(std::string)>
66 void function(std::string foo);
69 <p>So now you can update your alerter (for simplicity, lets go back to the
70 free-standing function version):</p>
72 <pre class="programlisting">
73 void warn_people(std::string where)
75 std::cout << "There are aliens in " << where << "!" << std::endl;
80 AlienDetector mydetector;
81 mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
91 <div class="navfooter">
93 <table width="100%" summary="Navigation footer">
95 <td width="40%" align="left">
96 <a accesskey="p" href="sect-using-mem-func.html">Prev</a> </td>
97 <td width="20%" align="center"><a accesskey="u" href="chapter-connecting.html">Up</a></td>
98 <td width="40%" align="right"> <a accesskey="n" href="sect-disconnecting.html">Next</a>
102 <td width="40%" align="left" valign="top">Using a member function </td>
103 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
104 <td width="40%" align="right" valign="top"> Disconnecting</td>