 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Amadeus W. M. Guest
|
Posted: Fri Sep 22, 2006 8:42 am Post subject: template and virtual |
|
|
Sometimes - more often than I would like - I run into situations
where I need a member function to be simultaneously templated and
virtual. I know it can be done, but I'm wondering if there's
some clever design trick that can circumvent this problem.
Example:
template <class Pixel_t>
class Image;
class BaseShape
{
public:
template <class Pixel_t>
void draw(Image<Pixel_t> & I); // draw self onto image.
};
class Circle : public BaseShape
{
public:
template <class Pixel_t>
void draw(Image<Pixel_t> & I); // circle specific.
};
etc.
I have a
list<BaseShape*> Shapes;
which I draw in a sweep:
list<BaseShape*>::iterator s;
for(s=Shapes.begin(); s!=Shapes.end(); s++)
s->draw(I);
This requires that draw() be virtual. Note that nothing else
but draw() needs to know anything about the pixel type (the
template parameter of Image), so I don't want to make the entire
BaseImage a template. Also, since Image is templated, I can't
derive all Images from one NON-templated common, abstract
BaseImage (and define a virtual void draw(BaseImage & I) const =0).
Is there any way to do what I want? I suspect there may be some
design flaw, otherwise I wouldn't be getting to this problem,
but I really like the idea of having each shape draw itself on
the image. If only the image was of a single type. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|