Thread: templates and inheritance problem

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    templates and inheritance problem

    Hi,

    I would like to create an abstract communication class which can be specialized for different communication subsystems. The communication class should be able to send special image data. The image class, however, is templated (over PixelType and Dimension). Therefore I have to use templates for my send method. I DO NOT want to use templates at class level, i.e.

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

    , because of two reasons:
    1) The communicator should be usable also in environments where no images have to be sent (and where they don't even exist).
    2) One single instance of the communicator class may have to send different images (with different PixelTypes). I don't want to list all of them as class templates (template <class ImageType1, class ImageType2, ..., class ImageTypeN>), even though the maximum number of N is known (at least for the current application, it might be different when the communicator class is used in other applications...).

    Usually (i.e. with non templated images), 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 my 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 for any help,

    Michael

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a policy class. Pass in different policy classes as a template parameter.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Thanks for your reply! Unfortunately I don't have any experience with policy classes and therefore didn't really understand how to use a policy class to solve my problem. At which level would I have to define the template(s) in order to pass in a policy class as a template parameter?

    Thanks, Michael

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    It depends on where you instantiate the object. In your case, one possible solution is at the lowest level.

    Kuphryn

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Unfortunately I was not quite precise with what I didn't understand from the first reply. My problem is, that I don't understand the basic concept you're proposing. What would such a policy class have to look like and in what sense does it help to solve my problem? I browsed some web-sites but didn't find any information on how to use policy classes in my case. Are you aware of any online ressources that might help me? Or can you discribe the basic idea behind the use of policy classes in my problem?

    Thanks, Michael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance and templates
    By misplaced in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 03:25 AM
  2. Dilemma with templates and inheritance..!
    By Halloko in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2004, 01:01 PM
  3. Templates and Inheritance problem
    By WebSnozz in forum C++ Programming
    Replies: 0
    Last Post: 04-11-2004, 02:39 PM
  4. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  5. Mixing templates and inheritance, and the damage caused ;)
    By SilentStrike in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 11:47 PM