Re: template specialization
From: Jeff (jeffz_2002_at_yahoo.com)
Date: 11/21/03
- Next message: C Johnson: "Re: Isn't 'vector' a misnomer?"
- Previous message: Niklas Borson: "Re: Isn't 'vector' a misnomer?"
- In reply to: Eric: "Re: template specialization"
- Next in thread: tom_usenet: "Re: template specialization"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Nov 2003 11:12:48 -0800
> > Second, if you're dealing with a class heirarchy, why do you need to
> > use templates here at all? Can't you just overload the function?
> > (pardon the silly question)
> > void DeliverEvent(Event* e);
> > void DeliverEvent(CircuitEvent* e);
>
> The problem is that Event and Circuit Event are base classes. There
> are several concrete classes that inherit from Event. Circuit Event
> also inherits from Event, and in turn, has several concrete classes
> inheriting from it. I use templates because I don't want to have to
> downcast to regain the subclass type.
I'm still puzzled as to why you need templates, though ... shouldn't
polymorphism do the trick? In Scott Meyers's "Effective C++, 2nd ed",
Item 41 says "Differentiate between inheritance and templates" -- the
section is aimed at class templates, but can be generalized to
functions as well. The summary of the item, copied from the book, is
here:
- A template should be used to generate a collection of classes when
the type of the objects /*does not*/ affect the behaviour of the
class's functions.
- Inheritance should be used for a collection of classes when the type
of the objects /*does*/ affect the behaviour of the class's functions.
Since you're talking about changing the behaviour of a particular
function depending on the type of item passed to it, polymorphism (or,
in this case, function overloading) seems to be called for...
But perhaps I'm missing something. Can you boil your problem down to
a simple representation and post it? That way I (and others) might be
able to comment more appropriately on your problem (perhaps creating a
new thread in this group, and copying the messages from this thread to
the new post).
Jeff
- Next message: C Johnson: "Re: Isn't 'vector' a misnomer?"
- Previous message: Niklas Borson: "Re: Isn't 'vector' a misnomer?"
- In reply to: Eric: "Re: template specialization"
- Next in thread: tom_usenet: "Re: template specialization"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|