Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the Visitor Pattern with template derived classes
    primarykey
    data
    text
    <p>I try to implement the Visitor pattern with templated derived classes</p> <h1><strong>I work with gcc 4.5</strong></h1> <p>here is the VisitorTemplate.hpp, I specialized Derived in the class Visitor, but I'd like to be able to handle any type:</p> <p>edit : thanks to the suggestions of interjay, the code compiles and runs without errors now</p> <pre class="lang-cpp prettyprint-override"><code>#ifndef VISITORTEMPLATE_HPP_ #define VISITORTEMPLATE_HPP_ #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; template&lt;class T&gt; Derived; class Visitor { public: virtual void visit(Derived&lt;string&gt; *e) = 0; }; class Base { public: virtual void accept(class Visitor *v) = 0; }; template&lt;class T&gt; Derived: public Base { public: virtual void accept(Visitor *v) { v-&gt;visit(this); } string display(T arg) { string s = "This is : " + to_string(arg); return s; } }; class UpVisitor: public Visitor { virtual void visit(Derived&lt;string&gt; *e) { cout &lt;&lt; "do Up on " + e-&gt;display("test") &lt;&lt; '\n'; } }; class DownVisitor: public Visitor { virtual void visit(Derived&lt;string&gt; *e) { cout &lt;&lt; "do Down on " + e-&gt;display("test") &lt;&lt; '\n'; } }; #endif /* VISITORTEMPLATE_HPP_ */ </code></pre> <p>main.cpp</p> <pre class="lang-cpp prettyprint-override"><code>Base* base = new Derived&lt;string&gt;(); Visitor* up = new UpVisitor(); Visitor* down = new DownVisitor(); base-&gt;accept(up); base-&gt;accept(down); </code></pre> <p>Now my goal is to use Derived in visit without specializing; unfortunately, visit is a virtual method so I can't template it</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.
 

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