Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be done in C++11 using variadic templates. Continuing from Pete's answer:</p> <pre><code>// Visitor template declaration template&lt;typename... Types&gt; class Visitor; // specialization for single type template&lt;typename T&gt; class Visitor&lt;T&gt; { public: virtual void visit(T &amp; visitable) = 0; }; // specialization for multiple types template&lt;typename T, typename... Types&gt; class Visitor&lt;T, Types...&gt; : public Visitor&lt;Types...&gt; { public: // promote the function(s) from the base class using Visitor&lt;Types...&gt;::visit; virtual void visit(T &amp; visitable) = 0; }; template&lt;typename... Types&gt; class Visitable { public: virtual void accept(Visitor&lt;Types...&gt;&amp; visitor) = 0; }; template&lt;typename Derived, typename... Types&gt; class VisitableImpl : public Visitable&lt;Types...&gt; { public: virtual void accept(Visitor&lt;Types...&gt;&amp; visitor) { visitor.visit(static_cast&lt;Derived&amp;&gt;(*this)); } }; </code></pre> <p>Subclasses of <code>Visitable</code>:</p> <pre><code>class Mesh : public Object, public VisitableImpl&lt;Mesh, Mesh, Text&gt; {}; class Text : public Object, public VisitableImpl&lt;Text, Mesh, Text&gt; {}; </code></pre> <p>A <code>Visitor</code> subclass:</p> <pre><code>class Renderer : public Visitor&lt;Mesh, Text&gt; {}; </code></pre> <p>It's not clear what the <code>value_type</code> of your <code>Scene</code> container is but you need to obtain a reference or pointer to <code>Visitable&lt;Mesh, Text&gt;</code> on which to call <code>accept</code>:</p> <pre><code>for(Scene::iterator it = scene.begin(); it != scene.end(); ++it) { Visitable&lt;Mesh, Text&gt;&amp; object = static_cast&lt;Visitable&lt;Mesh, Text&gt;&amp;&gt;(*it); if(pre_visit(object)) { object.accept(*this); post_visit(object); } } </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. 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