Note that there are some explanatory texts on larger screens.

plurals
  1. POC++. Templates and run time user information
    primarykey
    data
    text
    <p>I've been trying to learn more about generic programming since it's something I think I don't know enough about. So I'm thinking about how I would implement a template version of one of my programs. The program I am trying to do this with is a numerical integrator program in which the user selects which integrator to use (i.e. Euler, Runge Kutta, etc) and then integrates whatever function they choose. My current method of doing this is by having an abstract base class called Integrator, and several derived classes that implement the integration method. So the code would look something like this (much more going on but this is just to show the methodology). Note that I use Qt for this and I declare an Integrator *integrator; in the MainWindow class.</p> <pre><code>void MainWindow::on_integrateButton_clicked() { string whichIntegrator = getUserChoice(); integrator = getIntegrator( whichIntegrator, whichFunction, order ); integrator-&gt;setUp( data ); // things like initial conditions, time, step size, etc... runIntegratorInNewThread(); } </code></pre> <p>with getIntegrator essentially using the factory method</p> <pre><code>// x being current data, xdot being the results of evaluating derivatives typedef void (*pFunction)(const double t, const double x[], double *xdot); Integrator* getIntegrator( const string &amp;whichIntegrator, pFunction whichFunction, int order ) { if (whichIntegrator == "Euler") { return new Euler(whichFunction, order); } else if (whichIntegrator == "RungeKutta") { return new RungeKutta(whichFunction, order); } } </code></pre> <p>So this method works fine, and the integrator program runs very well. Now I know that template functions are generated at compile time and given that I'm using run time information, how would you implement this using templates? If the question is not clear, what I'm asking is... Given a user choice at run time, that being which integrator to use, how do I call the correct integration function using a template method?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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