I am sure that the answer is: βyou cannot use templates, you need to use virtual functions (dynamic polymorphism)β, but it looks like I would have to duplicate a lot of code if I went along this route, Here is the setting:
I currently have two classes: ColorImageSegmentation and GrayscaleImageSegmentation. They do essentially the same thing, but there are three differences - they work on different types (ColorImage and GrayscaleImage) - parameter, histogram dimension (3 vs 1) is different - PixelDifference function is different depending on the type of image
If I create a class
template <TImageType> class ImageSegmentation { };
I would be in good shape. However, I want this object to be a member of another class:
class MyMainClass { ImageSegmentation MyImageSegmentation; };
But the user needs to determine the type MyImageSegmentation (if the user opens the image in grayscale, I want to create an instance of MyImageSegmentation<GrayScaleType> . Similarly for the color image MyImageSegmentation<ColorType> .)
With derived classes, I could save the pointer and then do:
class MyMainClass { ImageSegmentation* MyImageSegmentation; }; ... user does something... MyImageSegmentation = new ColorImageSegmentation;
but how do I do something like this using templates? The problem is that I have a lot:
typedef TImageType::HistogramType HistogramType; typedef TImageType::PixelType PixelType;
something is happening, so I donβt know how to convert them into a dynamic polymorphic model without duplicating a whole bunch of code.
Sorry for the rambling ... does anyone have any suggestions for me?
Thanks,
David