title: How to create a unified interface for getters of an inheritance hierarchy of classes?
body: <p>I think that any explanation without the code would be just more obscure. So here is the code where I tried to keep everything as simple as possible.</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  class WithParametersBase
  {
  public:
      WithParametersBase();
  
      double getX() const {return 0.0;}
      double getY() const {return 1.0;}
  
      //let's say I want to access these members using an unified interface:
  
      double getParameter(int index) const;
  
      // For example index == 0 means getX and index == 1 means getY.
      // I could implement it for example like this:
  
  protected:
  
      void addGetter(double (WithParametersBase::* getter)()const)
      {
          getters_.push_back(getter);
      }
  
      std::vector&lt;double (WithParametersBase::*)()const&gt; getters_;
  };
  
  WithParametersBase::WithParametersBase()
  {
      addGetter(&amp;WithParametersBase::getX);
      addGetter(&amp;WithParametersBase::getY);
  }
  
  double WithParametersBase::getParameter(int index) const
  {
      return (this-&gt;*(getters_[index]))();
  }
  </code></pre>
  
  <p>Indeed it works. With a test program:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
     WithParametersBase base;
  
     std::cout &lt;&lt; base.getParameter(0)
               &lt;&lt; base.getParameter(1) &lt;&lt; std::endl;
  
     return 0;
  }
  </code></pre>
  
  <p>The printout is correct:</p>
  
  <pre><code>01
  </code></pre>
  
  <p>But in case I wnat to extend this class:</p>
  
  <pre><code>class WithParametersDerived : public WithParametersBase
  {
  public:
      WithParametersDerived();
      double getZ() const  {return 2.0;} // A new getter
  };
  
  WithParametersDerived::WithParametersDerived()
  {
      // I want to integrate the new getter into the previous interface
      addGetter(&amp;WithParametersDerived::getZ); 
  }
  </code></pre>
  
  <p>so that if I call:</p>
  
  <pre><code>WithParametersDerived derived;
  std::cout &lt;&lt; derived.getParameter(2) &lt;&lt; std::endl;
  </code></pre>
  
  <p>I want to get get a </p>
  
  <pre><code>2
  </code></pre>
  
  <p>I cannot compile the program. I get an error:</p>
  
  <pre><code>error: no matching function for call to
  'WithParametersDerived::addGetter
  (double (WithParametersDerived::*)()const)'
  </code></pre>
  
  <p>Which is reasonable, but I do not know how else to implement it.</p>
  
  <p>I want the creator of the derived class to be able to just add the new getter. I know, that it somehow doesn't feel right doing all this at runtime, but I do not see a template solution or a preprocessor solution. If you have some suggestions, please let me know. Anything! </p>