Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sounds like a <a href="http://en.wikipedia.org/wiki/Double_dispatch" rel="nofollow noreferrer">double dispatch</a> problem. Perhaps this would be a good place to implement the <a href="http://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">visitor pattern</a>?</p> <p>For example, create a class <code>Evaluator</code>, and for each <code>T</code> a subclass <code>ConcreteEvaluator&lt;T&gt;</code>. Give <code>A</code> and <code>B</code> methods that visit the <code>Evaluator</code>. Something like:</p> <pre><code>class Evaluator { virtual void visit_A(A* object); virtual void visit_B(B* object); }; template &lt;typename T&gt; class ConcreteEvaluator : public Evaluator { public: String* input_reference; T&amp; result_reference; ConcreteEvaluator(String&amp; input_reference_,T&amp; result_reference_) : input_reference(input_reference_), result_reference(result_reference_) {} virtual void visit_A(A* object) { object-&gt;Func(input_reference,result_reference); } virtual void visit_B(B* object) { object-&gt;Func(input_reference,result_reference); } } class A { ... virtual void apply_evaluator(Evaluator *eval) {eval-&gt;visit_A(this);} ... } class B { ... virtual void apply_evaluator(Evaluator *eval) {eval-&gt;visit_B(this);} ... } </code></pre> <p>For each subclass of <code>A</code>, a new method must be added to <code>ConcreteEvaluator</code>, so that this technique works best if <code>A</code>'s class hierarchy is stable. And for each subclass of <code>A</code>, it must have an <code>apply_evaluator</code> function defined properly.</p> <p>On the other hand, this may be total overkill. For about the same amount of work, you could always just pay the price to update <code>M::Evaluate</code>:</p> <pre><code>class M { ... void Evaluate(const String&amp; sInput, T&amp; tResult) { // try to downcast to each subclass of A. Be sure to check // sub-subclasses first try { dynamic_cast&lt;B*&gt;(pInterface.get())-&gt;Func(sInput, tResult); return; } catch (std::bad_cast&amp; ) { } ... // nothing worked. It must really be an A pInterface-&gt;Func(sInput,tResult); } ... }; </code></pre>
 

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