Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code below is a minimal working example of what you requested. <code>ClassA</code> emits two signals; <code>SigA</code> sends (and accepts) no parameters, <code>SigB</code> sends an <code>int</code>. <code>ClassB</code> has two functions which will output to <code>cout</code> when each function is called. In the example there is one instance of <code>ClassA</code> (<code>a</code>) and two of <code>ClassB</code> (<code>b</code> and <code>b2</code>). <code>main</code> is used to connect and fire the signals. It's worth noting that <code>ClassA</code> and <code>ClassB</code> know nothing of each other (ie they're not <em>compile-time bound</em>).</p> <pre><code>#include &lt;boost/signal.hpp&gt; #include &lt;boost/bind.hpp&gt; #include &lt;iostream&gt; using namespace boost; using namespace std; struct ClassA { signal&lt;void ()&gt; SigA; signal&lt;void (int)&gt; SigB; }; struct ClassB { void PrintFoo() { cout &lt;&lt; "Foo" &lt;&lt; endl; } void PrintInt(int i) { cout &lt;&lt; "Bar: " &lt;&lt; i &lt;&lt; endl; } }; int main() { ClassA a; ClassB b, b2; a.SigA.connect(bind(&amp;ClassB::PrintFoo, &amp;b)); a.SigB.connect(bind(&amp;ClassB::PrintInt, &amp;b, _1)); a.SigB.connect(bind(&amp;ClassB::PrintInt, &amp;b2, _1)); a.SigA(); a.SigB(4); } </code></pre> <p>The output:</p> <pre> Foo Bar: 4 Bar: 4 </pre> <p>For brevity I've taken some shortcuts that you wouldn't normally use in production code (in particular access control is lax and you'd normally 'hide' your signal registration behind a function like in KeithB's example). </p> <p>It seems that most of the difficulty in <code>boost::signal</code> is in getting used to using <code>boost::bind</code>. It <em>is</em> a bit mind-bending at first! For a trickier example you could also use <code>bind</code> to hook up <code>ClassA::SigA</code> with <code>ClassB::PrintInt</code> even though <code>SigA</code> does <em>not</em> emit an <code>int</code>:</p> <pre><code>a.SigA.connect(bind(&amp;ClassB::PrintInt, &amp;b, 10)); </code></pre> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload