Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Templates. Can't use inherited function
    primarykey
    data
    text
    <p>Well this has been giving me grief...</p> <pre><code>#include &lt;iostream&gt; class InterfaceClass { public: void test() { std::cout&lt;&lt;"Hello there.\n"; } }; template &lt;class T&gt; class TemplateClass { public: T t; }; class TestClass: public InterfaceClass {}; class TestInheritor { public: TemplateClass &lt; InterfaceClass &gt;* templateInherit; InterfaceClass* normalInherit; void test() { normalInherit-&gt;test(); templateInherit-&gt;t.test(); } }; int main (int nargs, char ** arg) { TestInheritor ti; ti.normalInherit = new TestClass; // THIS ONE COMPILES OKAY. //ti.templateInherit = new TemplateClass &lt;TestClass&gt;; // COMPILE ERROR. // THIS WORKS THOUGH TemplateClass &lt;TestClass&gt; * tempClass = new TemplateClass &lt;TestClass&gt;; ti.templateInherit=(TemplateClass &lt;InterfaceClass&gt;*)tempClass; // WHY DO I HAVE TO EXPLICITLY CAST? // OUTPUT WORKS AS EXPECTED. ti.test(); return 0; } </code></pre> <p>The normal inheritance example works just fine. The TestClass is automatically converted to a InterfaceClass. However, with the template example, it gives a compile error:</p> <pre><code>error: cannot convert 'TemplateClass&lt;TestClass&gt;*' to 'TemplateClass&lt;InterfaceClass&gt;*' in assignment </code></pre> <p>In my mind, it is obvious that you can convert <code>TemplateClass&lt;TestClass&gt;*</code> to <code>TemplateClass&lt;InterfaceClass&gt;*</code>... So what am I missing here?</p> <p>I can fix it by explicitly casting the template class to the base class, I am able to use the inherited test() function without any problem... So why am I required to explicitly cast the template class?</p> <p>Sorry if that's confusing... It's hard for me to explain this problem.</p> <hr> <p>Okay, I understand the issue a little more. I have decided to add a template to TestInheritor like so:</p> <pre><code>template &lt;class T2&gt; class TestInheritor { public: TemplateClass &lt; T2 &gt;* templateInherit; InterfaceClass* normalInherit; void test() { normalInherit-&gt;test(); templateInherit-&gt;t.test(); } }; int main (int nargs, char ** arg) { TestInheritor &lt;TestClass&gt; ti; ti.normalInherit = new TestClass; ti.templateInherit = new TemplateClass &lt;TestClass&gt;; ti.test(); return 0; } </code></pre> <p>Probably not the perfect solution, but it works for my purposes.</p> <hr> <p>Ah, and I see your solution:</p> <pre><code>#include &lt;iostream&gt; class InterfaceClass { public: void test() { std::cout&lt;&lt;"Hello there.\n"; } }; class TestClass: public InterfaceClass {}; template &lt;class T&gt; class TemplateClass { public: T t; }; template&lt;&gt; class TemplateClass&lt;TestClass&gt; : public TemplateClass&lt;InterfaceClass&gt; { public: }; class TestInheritor { public: TemplateClass &lt; InterfaceClass &gt;* templateInherit; InterfaceClass* normalInherit; void test() { normalInherit-&gt;test(); templateInherit-&gt;t.test(); } }; int main (int nargs, char ** arg) { TestInheritor ti; ti.normalInherit = new TestClass; ti.templateInherit = new TemplateClass &lt;TestClass&gt;; ti.test(); return 0; } </code></pre>
    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.
    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