1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 2. Connecting your code to signals</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="libsigc++"><link rel="up" href="index.html" title="libsigc++"><link rel="prev" href="ch01.html" title="Chapter 1. Introduction"><link rel="next" href="ch02s02.html" title="Using a member function"></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">Chapter 2. Connecting your code to signals</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch02s02.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="sec-connecting"></a>Chapter 2. Connecting your code to signals</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch02.html#idm45801113096800">A simple example</a></span></dt><dt><span class="sect1"><a href="ch02s02.html">Using a member function</a></span></dt><dt><span class="sect1"><a href="ch02s03.html">Signals with parameters</a></span></dt><dt><span class="sect1"><a href="ch02s04.html">Disconnecting</a></span></dt></dl></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idm45801113096800"></a>A simple example</h2></div></div></div><p>So to get some experience, lets look at a simple example...</p><p>Lets say you and I are writing an application which informs the user when
2 aliens land in the car park. To keep the design nice and clean, and allow for
3 maximum portability to different interfaces, we decide to use libsigc++ to
4 split the project in two parts.</p><p>I will write the <code class="literal">AlienDetector</code> class, and you will write the code to inform
5 the user. (Well, OK, I'll write both, but we're pretending, remember?)</p><p>Here's my class:</p><pre class="programlisting">
13 sigc::signal<void> signal_detected;
15 </pre><p>(I'll explain the type of signal_detected later.)</p><p>Here's your code that uses it:</p><pre class="programlisting">
18 cout << "There are aliens in the carpark!" << endl;
23 AlienDetector mydetector;
24 mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
30 </pre><p>Pretty simple really - you call the <code class="literal">connect()</code> method on the signal to
31 connect your function. <code class="literal">connect()</code> takes a <code class="literal">slot</code> parameter (remember slots
32 are capable of holding any type of callback), so you convert your
33 <code class="literal">warn_people()</code> function to a slot using the <code class="literal">slot()</code> function.</p><p>To compile this example, use:</p><pre class="programlisting">g++ example1.cc -o example1 `pkg-config --cflags --libs sigc++-3.0`</pre><p>Note that those `` characters are backticks, not single quotes. Run it with</p><pre class="programlisting">./example1</pre><p>(Try not to panic when the aliens land!)</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch02s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Using a member function</td></tr></table></div></body></html>