Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ template partial specialization - specializing one member function only
    text
    copied!<p>Bumped into another templates problem:</p> <p>The problem: I want to partially specialize a container-class (foo) for the case that the objects are pointers, and i want to specialize only the delete-method. Should look like this:</p> <p><strong>The lib code</strong></p> <pre><code>template &lt;typename T&gt; class foo { public: void addSome (T o) { printf ("adding that object..."); } void deleteSome (T o) { printf ("deleting that object..."); } }; template &lt;typename T&gt; class foo &lt;T *&gt; { public: void deleteSome (T* o) { printf ("deleting that PTR to an object..."); } }; </code></pre> <p><strong>The user code</strong></p> <pre><code>foo&lt;myclass&gt; myclasses; foo&lt;myclass*&gt; myptrs; myptrs.addSome (new myclass()); </code></pre> <p>This results into the compiler telling me that myptrs doesnt have a method called addSome. Why ?</p> <p>Thanx.</p> <p><hr/><h2><strong>Solution</strong></h2> based on tony's answer here the fully compilable stuff</p> <hr/> <p><strong>lib</strong></p> <pre><code>template &lt;typename T&gt; class foobase { public: void addSome (T o) { printf ("adding that object..."); } void deleteSome (T o) { printf ("deleting that object..."); } }; template &lt;typename T&gt; class foo : public foobase&lt;T&gt; { }; template &lt;typename T&gt; class foo&lt;T *&gt; : public foobase&lt;T *&gt; { public: void deleteSome (T* o) { printf ("deleting that ptr to an object..."); } }; </code></pre> <p><strong>user</strong></p> <pre><code>foo&lt;int&gt; fi; foo&lt;int*&gt; fpi; int i = 13; fi.addSome (12); fpi.addSome (&amp;i); fpi.deleteSome (12); // compiler-error: doesnt work fi.deleteSome (&amp;i); // compiler-error: doesnt work fi.deleteSome (12); // foobase::deleteSome called fpi.deleteSome (&amp;i); // foo&lt;T*&gt;::deleteSome called </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