Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing member function pointer to class-less function
    text
    copied!<p>In the code below, I cannot figure out a way of passing a member function to a generic root-finder.</p> <pre><code>#include &lt;stdio.h&gt; double OneDimBisector(double (*fun)(float), float a, float b, float tol){ double val; val = (*fun)(0.5*(b-a)); // actually: do proper bisection return val; } class EOS { public: double S_array[10][10]; // actually: filled by constructor double S(double T, double P); double T_PS(double P, double S); double functForT_PS(double T); double (EOS::*pfunctForT_PS)(double); double Sseek, Pseek; }; double EOS::S(double T, double P){ double val = T+P; // actually: interpolate in S_array return val; } double EOS::functForT_PS(double T){ return S(T,Pseek)-Sseek; } // Find T from P and S (T is invertible), assuming the intervals are ok double EOS::T_PS(double P, double S0){ double Tmin = 2., Tmax = 7., T1, tol=1e-8; pfunctForT_PS = &amp;EOS::functForT_PS; Sseek = S0; Pseek = P; printf("\n %f\n", (*this.*pfunctForT_PS)(4.)); // no problem T1 = OneDimBisector(pfunctForT_PS, Tmin, Tmax, tol); // wrong type for pfunctForT_PS return T1; } int main() { double P=3., S=8; EOS myEOS; printf("\n %f %f %f\n",P,S,myEOS.T_PS(P,S)); } </code></pre> <p>I do not want to make the root-finder a member because it is not specific to this class, and the solution of making everything <code>static</code> seems very inelegant. Would someone have an idea? This must be a common situation yet I did not find a relevant post that was also understandable to me.</p> <p>Thanks!</p> <p><strong>Edit:</strong> Actually, I also meant to ask: Is there a proper, thread-safe way of setting the <code>Pseek</code> variable other than what I did? Just to make it clear: I am doing one-dimensional root finding on a two-dimensional function but fixing one of the two arguments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload