[Insight-users] Problems with Templates and Inheritance

Michael michakuhn at gmx.ch
Fri Jun 11 09:20:13 EDT 2004


  Hi,

I would like create an abstract communication class (let's call it 
Communicator), which provides an interface consisting of some point to 
point communication methods. It should then be possible to derive 
different specialized Communicator classes from this abstract class (for 
example SocketCommunicator, MPICommunicator, ...). Among the point to 
point communication methods, I'd like to have a method for sending 
(ITK-) image data. Something like:

Send(const ImageType* image, ...)

Since itkImage is a templated class, I have to use templates to define 
the parameter image. I don't want to use templates at class level, i.e.

template <class ImageType>
class Communicator {
...
}

, for two reasons:

1) the communicator should be usable in enviorenments where no images 
have to be sent (and where they don't even exist)
2) one single instance of the communicator may have to send different 
images (of different types). I don't want to list all of them as 
templates (template<class ImageType1, class ImageType2, .., class 
ImageTypeN.>), even though the number of N is known for my particular 
application, since it might be different when the same communicator 
class should be reused in another environment.

Usually (i.e. with non templated parameters), I would just define a send 
method in the abstract communicator class which uses the image 
superclass (let's call it ImageSuperclass) as a parameter:

virtual void Send(ImageSuperclass* image, ...) = 0;

This method could then send any derived image classes, say Short2DImage, 
Char3DImage, ...

Then I would derive my specialized Communicator class, say 
MPICommunicator, and this class would override the send method in the 
abstract class. However, since the image class is templated, my method 
has to look something like

template <class ImageType>
void Send(ImageType* image, ...)

Unfortunately, templated member functions cannot be declared as virtual, 
and can therefore not be overwritten by my derived MPICommunicator class.

Can anybody explain me how I can define an abstract communicator class 
that defines a send method for the templated images and from which I can 
derive different implementations of communicator classes 
(MPICommunicator, SocketCommunicator, ...). The derived classes 
obviously somehow have to reimplement the send method of the abstract 
superclass.

Thanks,

Michael



More information about the Insight-users mailing list