title: const function pointers
body: <p>I'm have some difficulties with function pointers. I have an base class which defines a function pointer that via <strong>typedef double (*function)(double *x) const;</strong></p>
  
  <ul>
  <li><p>A quick side question: why does the above typedef not compile?</p>
  
  <p>Gives the following error: <strong>error: ‘const’ and ‘volatile’ function
  specifiers on ‘function’ invalid in type declaration</strong></p></li>
  </ul>
  
  <p>For the part below I use <strong>typedef double (*function)(double *x)</strong>.
  Now each daughter class can implement multiple and different versions of functions of this type. Via an enum I select the function of my choice, which sets my non-member function pointer (defined in the base class) to be initialized by one of these member-function pointers of the daughter class. Here's a code snippet:</p>
  
  <p>The source file of the daughter class:</p>
  
  <pre class="lang-c prettyprint-override"><code>PndLmdROOTDataModel1D::PndLmdROOTDataModel1D(interpolation_type intpol_type) {
    if(intpol_type == CONSTANT) {
      setModelFunction(&amp;PndLmdROOTDataModel1D::evaluateConstant); 
    }
    else if (intpol_type == SPLINE) {
      setModelFunction(&amp;PndLmdROOTDataModel1D::evaluateSpline);
    }
    else {
      setModelFunction(&amp;PndLmdROOTDataModel1D::evaluateLinear);
    }
  }
  </code></pre>
  
  <p>And the base Class (header file):</p>
  
  <pre class="lang-c prettyprint-override"><code>class MultiModel1D: public Model1D {
  protected:
    function model_func;
  
  public:
    MultiModel1D();
    virtual ~MultiModel1D();
  
    void setModelFunction(function f);
  }
  </code></pre>
  
  <p>When compiling I get the following error:</p>
  
  <blockquote>
    <p>note:   no known conversion for argument 1 from ‘double
    (PndLmdROOTDataModel1D::*)(double*)’ to ‘function {aka double
    (*)(double*)}’</p>
  </blockquote>
  
  <p>I'm using the function pointer, because of speed issues (at least I think this should be faster than constantly running through some switch case). What am I doing wrong? Maybe there is also some design pattern that will serve as a better alternative... Thanks in advance!</p>
  
  <p>Steve</p>