Re: Design problem with inheritance
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Fri, 14 Jul 2006 19:59:53 +0200
On 14 Jul 2006 07:36:44 -0700, Alvin Ryder wrote:
Dmitry A. Kazakov wrote:
On 13 Jul 2006 14:31:51 -0700, Alvin Ryder wrote:
I would not use inheritence in this way.
1. When you need to vary types use generics or templates if your
language supports them.
Well, because this, IMO wrong, advise has repeated ... no! Generics expose
a static form of polymorphism. This automatically excludes any possibility
Frankly I'm only guessing at what the OP wants, I should've made that
clear. But do you not agree parametric types are useful when you want
the type to vary, as in stack<int> and stack<char>?
I'd prefer a dynamically polymorphic variant where stack would implement a
container interface. Unfortunately, it is impossible with contemporary type
systems. Generics provide polymorphism for poor.
a static form of polymorphism. This automatically excludes any possibility
to have a container of polymorphic objects. With generics the container
*itself* will be polymorphic, its elements will be not. It seems that the
OP wished a different thing: a non-polymorphic container of polymorphic
elements.
And in C++ you /can/ use polymorphism and inheritence in the parametric
type, as in
class Base has virtual operation1()
template <class T> class Derived1 : public Base
template <class T> class Derived2 : public Base
// Then
vector<Base*> vectorX;
vectorX.push_back( new Derived1<int> ); // Now its an int
vectorX.push_back( new Derived2<char>); // Now its a char
// Now call operation1 on each ... its polymorphic.
Here Base* is dynamically polymorphic. Generics play a side role of a macro
to automate creation of new types derived from Base.
I'm not sure why you're saying you can't contain polymorphic objects?
I meant that if you have stack generic, then you cannot put different types
onto the same stack. I.e. the stack element is not polymorphic, all
elements of a given stack have the same specific type.
Ok I'm probably misunderstanding you as well? Nevermind, I've had a 16
hour day ... I'm going to bed.
Sleep tight! (:-))
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- References:
- Design problem with inheritance
- From: srinivasarao_moturu
- Re: Design problem with inheritance
- From: Alvin Ryder
- Re: Design problem with inheritance
- From: Dmitry A. Kazakov
- Re: Design problem with inheritance
- From: Alvin Ryder
- Design problem with inheritance
- Prev by Date: Re: Persistence
- Next by Date: Re: Poly Couples
- Previous by thread: Re: Design problem with inheritance
- Index(es):
Relevant Pages
|