title: Observer pattern with type information (C++)
body: <p>i'm trying to implement a custom version of an Observer pattern in c++.</p>
  
  <p>These are my classes (just interface):</p>
  
  <pre><code>class Observer{
  public:
      void observe(std::string behaviour, Observable * observable);
      void stopObserving(std::string behaviour, Observable * observable);
      virtual void notified(std::string behaviour, Observable * observed);
  // ...
  };
  
  class Observable{
  public:
      void notifyBehaviour(std::string behaviour);
      // ...
  };
  </code></pre>
  
  <p>And can be used like this:</p>
  
  <pre><code>class A : public Observer{
  public:
      void notified(std::string behaviour, Observable * observed){
          if (behaviour == "jumping"){
              // ...
          }
      }
  };
  
  class B : public Observable{
  public:
      void jump(){
          // ...
          notifyBehaviour("jumping");
      }
  };
  
  int main(){
      A a;
      B b;
      a.observe("jumping", &amp;b);
      b.jump();
      // ...
  }
  </code></pre>
  
  <p>Each class implementing Observer can register itself as observing an Observable with a behaviour.</p>
  
  <p>The Observable can notify its actions with "notifyBehaviour" to everyone interested.</p>
  
  <p>Each Observer listening will be notified via its method "notified".</p>
  
  <p>The above works perfectly. Anyway in the example above:</p>
  
  <pre><code>void notified(std::string behaviour, Observable * observed){
      if (behaviour == "jumping"){
      // ...
  }
  </code></pre>
  
  <p>i'd like to use the Observable * i'm passing to do something on the notifying object. Unfortunately i'll have to do something like this:</p>
  
  <pre><code>void notified(std::string behaviour, Observable * observed){
      if (behaviour == "jumping"){
          auto pointer = dynamic_cast&lt;B *&gt;(observed);
          if (pointer)
              pointer-&gt;doSomethingElse();
      }
  }
  </code></pre>
  
  <p>That's ugly and will probably create problems. For example observing two different jumping entities will need multiple castings waiting for the right one.</p>
  
  <p>My question is: is it possible to pass some RTTI or call an overloaded function or a template having already the right object type? I'd like to have:</p>
  
  <pre><code>class A : public Observer{
  public:
      void notified(std::string behaviour, B * observed){
          if (behaviour == "jumping"){
              observed-&gt;doSomethingBlike();
              // observed if of type B !
          }
      }
      void notified(std::string behaviour, C * observed){
          if (behaviour == "jumping"){
              observed-&gt;doSomethingClike();
              // observed if of type C !
          }
      }
  };
  </code></pre>
  
  <p>so i just need to implement the various object types i'd like to listen.</p>
  
  <p>I've tried with inheritance, templates, boost::any but still without luck.</p>
  
  <p>Thanks for ideas.</p>