Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Beware of diamond inheritance heirarchies. Also note that overloading virtual functions is a bad thing. So if you have something like this:</p> <pre><code>class Handler : public EventHandler&lt;int&gt;, public EventHandler&lt;string&gt; { ... }; </code></pre> <p>Which changeEvent() function will be called? Don't count on it!</p> <p>If you are careful the above code should be fine, but if you want to avoid inheritance altogether then I suggest using function references associated with some unique identifier. As an example:</p> <pre><code>class Listener { public: virtual ~Listener ( ) { } }; template&lt;typename Event&gt; class Distributor : public Listener { public: void addListener (shared_ptr&lt;Listener&gt;, function&lt;void (Event)&gt;); void listen (Event e) { for_each(_listeners.begin(), _listeners.end(), bind(&amp;ListenNode::listen, _1, e)); } private: struct ListenNode { weak_ptr&lt;Listener&gt; listener; function&lt;void (Event)&gt; callback; void listen (Event e) { shared_ptr&lt;Listener&gt; l = listener.lock(); if(l) callback(e); } }; list&lt;ListenNode&gt; _listeners; }; </code></pre> <p>With this setup, all listeners derive from one base class virtually. Listeners can have multiple callbacks registered, and Distributors can be chained. Of course you don't have to use shared_ptr's but I like them because they save from the hassle of unregistering listeners. You can register the callbacks any way you like, associating them with a string, integer or whatever.</p> <p>I have omitted a lot of detail, event distribution is a complicated business. I think Andrei Alexandrescu wrote a detailed article on the topic, look it up. </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.
    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