title: C++ containers, covariance and template
body: <p>I have some issues to find a relevant solution to my problem.
  I have to return some data from a class, and the type of data kind of depends on the class.
  My first solution was this :</p>
  
  <pre><code>class Base
  {
      virtual QVector&lt;Data*&gt; getData() const = 0;
  };
  
  class Derived : public Base
  {
     virtual QVector&lt;DerviedData*&gt; getData() const = 0;
  };
  </code></pre>
  
  <p>But I know this is impossible, even if DerivedData extends Data, because of invalid covariant return types.</p>
  
  <p>So I came up with another idea which implies template. What I did is I turned the Base class into a template class :</p>
  
  <pre><code>template&lt;class T&gt;
  class Base
  {
      virtual QVector&lt;T*&gt; getData() const = 0;
  }
  </code></pre>
  
  <p>And then I could write a <code>Derived</code>constructor like that :</p>
  
  <pre><code>Derived::Derived() : Base&lt;DerivedData&gt;() {}
  </code></pre>
  
  <p>But know I have another problem. Suppose that I write another class, which has a method taking any Base class in parameters.</p>
  
  <pre><code>void Foo::doSomething(Base* b) { 
      b-&gt;getData(); 
  }
  </code></pre>
  
  <p>This does not compile and says </p>
  
  <p><i>invalid use of template-name 'Base' without an argument list</i> </p>
  
  <p>which I understand perfectly. </p>
  
  <p>Supposing that my code will look like that :</p>
  
  <pre><code>DerivedClass1 d1;
  DerivedClass2 d2;
  DerivedClass3 d3;
  
  this-&gt;doSomething(&amp;d1);
  this-&gt;doSomething(&amp;d2);
  this-&gt;doSomething(&amp;d3);
  </code></pre>
  
  <p>What are my solutions here ? May I do something like "templating" the method doSomething ? </p>
  
  <pre><code>this-&gt;doSomething&lt;DerivedData&gt;(&amp;d1);
  </code></pre>
  
  <p>With a protoype like </p>
  
  <pre><code>template&lt;class T&gt;
  void doSomething(Base&lt;T&gt;* b);
  </code></pre>
  
  <p>Is that possible ? Is that a good way of thinking ?
  Coming from Java, I used to resolve such problems by using wildcards </p>
  
  <pre><code>abstract List&lt;? extends Data&gt; getData();
  </code></pre>
  
  <p>But I heard there is stricly speaking no such things in C++ (more or less simulable with such things as  <code>std::is_base_of</code>).</p>
  
  <p>Thank you for your time.</p>