title: Generic observer pattern in C++
body: <p>In many cases in my application i need class A to register itself as a listener on class B to receive notification when something happens. In every case i define a separate interface B implements and A can call do. So for example, A will have the following method:</p>
  
  <pre><code>void registerSomeEventListener(SomeEventListener l);
  </code></pre>
  
  <p>Also, in many cases, B will need to support multiple listeners so i reimplement the registration and notifyAll logic.</p>
  
  <p>One generic way i know is to have some EventListener (implement by A) and EventNotifier (implement by B) classes. In this case each event is identified by a string and A implements the method:</p>
  
  <pre><code>void eventNotified(string eventType);
  </code></pre>
  
  <p>I think this is not a good solution. It will result in many if-else statements in case A listens to several events and might result in bugs when event names are changed only in the listener or the notifier.</p>
  
  <p>I wonder what is the correct way to implement the observer pattern in C++?</p>