Getting derived class objects out of a vector<Parent*>
<p>I know that you can store a bunch of Parent* objects in a vector. However, if the functions you need to call on the objects cannot be defined in the parent class (they depend on a template parameter of the subclass) then how are you supposed to retrieve the objects from the container?</p>

<p>In this example:</p>

<pre><code>#include &lt;iostream&gt;

class ImageBase{};

template &lt;typename TPixel&gt;
class Image : public ImageBase
{
public:
  TPixel GetPixel() const {TPixel a; return a;}
};

template&lt;typename TImage&gt;
void Output(const TImage* image)
{
  std::cout &lt;&lt; image-&gt;GetPixel();
}

int main(int, char *[])
{
  // Works correctly
  {
  Image&lt;float&gt;* image = new Image&lt;float&gt;;
  image-&gt;GetPixel();
  Output(image);
  }

  {
  ImageBase* image = new Image&lt;float&gt;;
  Output(image);
  }

  return 0;
}
</code></pre>

<p>The Output(image); where 'image' is a ImageBase* fails (of course) because GetPixel is not defined in ImageBase. I know you can dynamic_cast&lt;> to a bunch of types to figure out if the subclass matches any of them, but this list could very quickly get very long. The long list would be fine if it could reside in one place, but how would you make a function to do this? The function would take an ImageBase*, but what would it return?</p>

<pre><code>returnType? GetSubclass(ImageBase* input)
{
  if(dynamic_cast&lt;Image&lt;float&gt;*&gt;(input))
    {
    return Image&lt;float&gt;*;
    }
  else if(dynamic_cast&lt;Image&lt;int&gt;*&gt;(input))
    {
    return Image&lt;int&gt;*;
    }
}
</code></pre>

<p>It seems reasonable to me to want to be able to call some template functions on subclasses that only vary in signature by their template parameter (as setup in this example), does it not?</p>

<p>In my real case, both Image and ImageBase are part of a library, so I cannot change them.</p>
