Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not really the best way to do this, either from a pure OO point of view or a functional or procedural POV. First of all, your class A is really nothing more than a namespace that has to be instantiated. Personally, I'd just put its functions as free floating C-style ones - maybe in a namespace somewhere so that you get some kind of classification.</p> <p>Here's how you'd do it in pure OO:</p> <pre><code>class Function { virtual double Execute(double value); }; class Function1 : public Function { virtual double Execute(double value) { ... } }; class FunctionProcessor { void Process(Function &amp; f) { ... } } </code></pre> <p>This way, you could instantiate <code>Function1</code> and <code>FunctionProcessor</code> and send the Function1 object to the Process method. You could derive anything from <code>Function</code> and pass it to Process.</p> <p>A similar, but more generic way to do it is to use templates:</p> <pre><code>template &lt;class T&gt; class FunctionProcessor { void Process() { T &amp; function; ... } } </code></pre> <p>You can pass anything at all as T, but in this case, T becomes a compile-time dependency, so you have to pass it in code. No dynamic stuff allowed here!</p> <p>Here's another templated mechanism, this time using simple functions instead of classes:</p> <pre><code>template &lt;class T&gt; void Process(T &amp; function) { ... double v1 = function(x1); double v2 = function(x2); ... } </code></pre> <p>You can call this thing like this:</p> <pre><code>double function1(double val) { return blah; } struct function2 { double operator()(double val) { return blah; } }; // somewhere else FunctionProcessor(function1); FunctionProcessor(function2()); </code></pre> <p>You can use this approach with anything that can be called with the right signature; simple functions, static methods in classes, functors (like <code>struct function2</code> above), <code>std::mem_fun</code> objects, new-fangled c++11 lambdas,... And if you use functors, you can pass them parameters in the constructor, just like any object.</p> <p>That last is probably what I'd do; it's the fastest, if you know what you're calling at compile time, and the simplest while reading the client code. If it has to be extremely loosely coupled for some reason, I'd go with the first class-based approach. I personally think that circumstance is quite rare, especially as you describe the problem.</p> <p>If you still want to use your <code>class A</code>, make all the functions static if they don't need member access. Otherwise, look at <code>std::mem_fun</code>. I still discourage this approach.</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.
    1. VO
      singulars
      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