title: Polymorphism and function binding
body: <p>For an event system i'm writing i want to bind callbacks to a list of functions.  </p>
  
  <p>Here is a basic example of what i want to do: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  #include &lt;string&gt;
  
  class Base {
    public:
      virtual std::string getType() const = 0;
  };
  
  class Derived : public Base {
    protected:
      int some_data;
  
    public:
  
      Derived(int some_data): some_data(some_data) {}
  
      virtual std::string getType() const {
        return "Derived";
      }
  
      int getData() const {
        return this-&gt;some_data;
      }
  };
  
  class DerivedTwo : public Base {
    protected:
      double some_data;
  
    public:
  
      DerivedTwo(double some_data): some_data(some_data) {}
  
      virtual std::string getType() const {
        return "DerivedTwo";
      }
  
      // The type of data is not always the same.
      double getData() const {
        return this-&gt;some_data;
      }
  };
  
  // The type of member should ALWAYS be Derived but then i can't store it in &lt;callback&gt;
  void onDerivedEvent(Base&amp; member) {
    std::cout &lt;&lt; member.getType() &lt;&lt; std::endl;
  
    // This is obviously not possible with member being a base class object
    // member.getData();
  }
  
  // The type of member should ALWAYS be DerivedTwo but then i can't store it in &lt;callback&gt;
  void onDerivedTwoEvent(Base&amp; member) {
    std::cout &lt;&lt; member.getType() &lt;&lt; std::endl;
  }
  
  int main() {
    std::function&lt;void(Base&amp;)&gt; callback;
  
    callback = std::bind(onDerivedEvent, std::placeholders::_1);
    callback(Derived(2));
  
    callback = std::bind(onDerivedTwoEvent, std::placeholders::_1);
    callback(DerivedTwo(3.0));
  
    return 0;
  }
  </code></pre>
  
  <p>The only thing i would like to change is that <code>onCallback()</code> should take a derived class member as argument instead of a reference to a base object, so i can call <code>getData()</code> for example.<br>
  In this example this would mean: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>void onCallback(Derived&amp; derived);
  </code></pre>
  
  <p>However, if i do this, i can no longer <code>bind()</code> the method to <code>callback</code> because the argument types are not matching.</p>
  
  <p>Does anyone know how to make this work?  </p>
  
  <p><strong>// EDIT</strong><br>
  Sorry for the confusion here, i updated the source code with some more specifics and examples to maybe clarify what im doing here.</p>
  
  <p><strong>Note:</strong><br>
  Since it seems like this is very relevant, here is the specific use case for what i'm trying to do here:<br>
  It's part of an event system for an engine i'm building. There are basic events pre-defined but it should be extendable with more specific events by a user using this engine. So there is not definitive list of derived classes. Then some object can subscribe to a specific event type and whenever the central event bus recieves such an event, it calls all subscribed callback functions with the event as argument. The reason i am not adding a one and for all handle function in the derived class is, the events an be used in multiple ways.</p>
  
  <p><strong>Answers to some questions from the comments:</strong>  </p>
  
  <blockquote>
    <p>What should happen if you pass onCallback an object that isn't that specific Derived&amp;? (ie, add a Derived2 which has a doStuff2. Pass it to callback. What do you want to happen?
    That should not be possible. </p>
  </blockquote>
  
  <p>I might have not calrified that and also had a misleading information at the beginning which i have editted since then. The type of the passed derived class is always known beforehand. For example: <code>onKeyEvent</code> will always recieve a KeyEvent object and not a base class object or any other derived variants.<br>
  However, the variable to which this function is bound should be able to store functions which accept different derived classes from <code>Base</code></p>
  
  <p>This is my storage for all events:  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>std::map&lt;EventType, std::list&lt;std::function&lt;void(const Event&amp;)&gt;&gt;&gt; listener_map;
  </code></pre>
  
  <blockquote>
    <p>Why isn't onCallback a method in Base that Derived overrides </p>
  </blockquote>
  
  <p>I answered this in a comment. <code>...The reason i am not adding a one and for all handle function in the derived class is, the events an be used in multiple ways...</code><br>
  Meaning, i might have an <code>KeyEvent</code> which has the data to a key (which key, is it pressed/released/held) and the listening function(s) can use this data for whatever it wants. (Check if some specific key is pressed, chech if any random key is pressed and so on.) Some other events might not have any data at all and just notify a listener that something happened or have multiple sets of data etc.</p>
  
  <blockquote>
    <p>Is there, or can there be, a finite, bounded at compile time, central list of all of the types that derive from Base at any point in your code?</p>
  </blockquote>
  
  <p>In theory yes. During compilation there will be a finite number of Derived classes. However these might be different for the compilation of the library and the compilation of the project using this library.</p>