Re: template specialization

From: Jeff (jeffz_2002_at_yahoo.com)
Date: 11/21/03


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



Relevant Pages

  • Re: The Language I want
    ... inheritance of interfaces from concrete types. ... Generics is a huge mess. ... Templates or hygenic macros are not generics! ...
    (comp.lang.misc)
  • Re: CPU recommendations?
    ... extern void startMotor; ... but MI (especially interface inheritance) ... Things like RTTI and exceptions give little value for money (or source code clarity or generated code quality), ... I've seen templates that provide neat and flexible ...
    (comp.arch.embedded)
  • Re: CPU recommendations?
    ... extern void startMotor; ... the interface definition. ... generated or used for templates, ... Interface inheritance - In this ...
    (comp.arch.embedded)
  • Re: How to start with MFC
    ... techniques, such as exceptions, inheritance and templates. ... and they are asking you to learn MFC!? ... BTW, if subject is C++, I would not say that exceptions, inheritance ... C is a dead language. ...
    (microsoft.public.vc.mfc)
  • Re: The Language I want
    ... inheritance of interfaces from concrete types. ... modules and packages. ... Templates or hygenic macros are not generics! ...
    (comp.lang.misc)