Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you could look at the visitor pattern. this is the general solution to 'double dispatch' type problems (dispatch to a virtual function based on the object <em>and</em> the message). </p> <p>That is, put <code>foo()</code> into a visitor, and rename <code>A::foo()</code> to <code>A::Visit(FooVisitor&amp; )</code>:</p> <p>edit: to clarify, it might help to disentangle the purposes of your hierarchies. If you think about it, you are trying to model the relationship of one hierarchy (<code>AA</code> and <code>BB</code>) in terms of another (<code>A</code> and <code>B</code>). This is pretty awkward to model, or even to think about conceptually. </p> <p>To remodel this as a visitor, you'd generally turn one of the hierarchies into a single class, and instead model the <em>operations</em> you can perform on that class in a hierarchy of algorithms via a visitor. This is more robust because it forces you to explicitly implement each combination of hierarchy relationships, and will break at compile-time (good) if you modify the hierarchy later on. </p> <pre><code>class A; class B; struct AVisitor { virtual ~AVisitor() { } virtual void Visit(A&amp; ) = 0; virtual void Visit(B&amp; ) = 0; }; class A { public: virtual ~A() { } virtual void Visit(AVisitor &amp; visitor) { visitor.Visit(*this); } }; class B : public A { public: virtual void Visit(AVisitor &amp; visitor) { visitor.Visit(*this); } }; struct PrintingVisitor : public AVisitor { void Visit(A&amp; a){cout&lt;&lt;"A::foo"&lt;&lt;endl;} void Visit(B&amp; b){cout&lt;&lt;"B::foo"&lt;&lt;endl;} }; int main() { B b; PrintingVisitor printer; b.Visit(printer); } </code></pre>
    singulars
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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