In the following code:
#include "itkImage.h"
#include "itkImageRegionIterator.h"
struct CustomImageBase
{
virtual void DoSomething() = 0;
};
template <typename TPixel>
struct CustomImage : public itk::Image<TPixel, 2>, public CustomImageBase
{
void DoSomething()
{
itk::Index<2> index;
index.Fill(0);
std::cout << this->GetPixel(index);
}
};
int main(int, char *[])
{
std::vector<CustomImageBase*> images;
CustomImage<float>* floatImage = CustomImage<float>::New().GetPointer();
CustomImage<int>* intImage = CustomImage<int>::New().GetPointer();
return EXIT_SUCCESS;
}
I get the error: invalid convsion from itk::Image* to CustomImage*
Note that this works fine:
itk::Image<float>* testFloatImage = itk::Image<float>::New().GetPointer();
Since CustomImage inherits from itk::Image, I don’t understand the problem?
If
Bderives fromA, you cannot convertA*toB*. IfCalso derives fromA, and yourA*really points to aCobject instead of aBobject, what would happen if you pretend it is aB? The conversion is unsafe. In your case, you know thatGetPointer()will always return aCustomImage<T>. The compiler does not know that. You can tell it by using a cast:Edit: this is precisely why the conversion is unsafe: after reading the comments on your question, I don’t believe
CustomImage<float>::New().GetPointer()really points to aCustomImage, and if not, the cast would simply turn compiler errors into runtime errors.