C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template and virtual

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Amadeus W. M.
Guest





PostPosted: Fri Sep 22, 2006 8:42 am    Post subject: template and virtual Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.