Note that there are some explanatory texts on larger screens.

plurals
  1. POTemplate polymorphism
    text
    copied!<p>I've recently received a <a href="https://stackoverflow.com/questions/13454756/c-polymorphic-object-members">very good answer as how to overload specific class members</a> by deriving classes.</p> <p>The issue now is that one of the members is in fact templated with specializations either being <code>BaseClass</code>, <code>DerivedClass</code> or <code>Derived2Class</code></p> <pre><code>#include &lt;iostream&gt; using std::cin; using std::cout; using std::endl; template&lt;class T&gt; class Queue { public: Queue(); Queue(T*); } class Com { public: virtual void setReady() { cout &lt;&lt; "Com" &lt;&lt; endl; } }; class DerivedCom : public Com { public: void setReady() { cout &lt;&lt; "DCom" &lt;&lt; endl; } }; class Derived2Com : public Com { public: void setReady() { cout &lt;&lt; "D2Com" &lt;&lt; endl; } }; class BaseClass { protected: Com* com; Queue&lt;BaseClass&gt;* queue; public: BaseClass(Com* c = new Com, Queue&lt;BaseClass&gt;* q = new Queue&lt;BaseClass&gt;) : com(c), queue(q) { } void setReady() { com-&gt;setReady(); } }; class DerivedClass : public BaseClass { public: DerivedClass() : BaseClass(new DerivedCom, new Queue&lt;DerivedClass&gt;) { } }; class Derived2Class : public BaseClass { public: Derived2Class() : BaseClass(new Derived2Com, new Queue&lt;Derived2Class&gt;) {} }; int main() { BaseClass* base = new Derived2Class(); base-&gt;setReady(); return 0; } </code></pre> <p>I can, without problem, "overload" simple classes like <code>Com</code>, <code>DerivedCom</code> and <code>Derived2Com</code> but the constructor signature of <code>BaseClass</code> won't fit to the type the deriving classes are trying to send it.</p>
 

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