There are two classes, ImageBase, and template <T> Image in a library I'm using. I want to store many of these images in a container, and then pass them to a function templates that expect an Image<T>. The only thing I knew to do was store ImageBase* in the container:
#include <iostream>
#include <vector>
class ImageBase { };
template <typename TPixel>
class Image : public ImageBase
{
public:
TPixel GetPixel() const
{
TPixel a = 3; return a;
}
};
template<typename TImage>
void DoSomething(const TImage* image)
{
TPixel pixel = image->GetPixel(); // Dummy call to an Image function
}
int main(int, char *[])
{
std::vector<ImageBase*> images;
ImageBase* image = new Image<float>;
images.push_back(image);
ImageBase* image2 = new Image<int>;
images.push_back(image2);
//DoSomething(images[0]); // no member named GetPixel
return 0;
}
but of course passing an ImageBase to a function that calls a function expecting a type Image (using functions defined in Image and not in ImageBase) doesn't make sense. Of course I could add pure virtual functions in ImageBase and implement them in Image, but then I will be using a modified version of the library, which is huge headaches for users. Is there a way to do this without touching ImageBase?
Thanks,
David