Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!pt.cs.cmu.edu!pt!dld
From: dld@F.GP.CS.CMU.EDU (David Detlefs)
Newsgroups: comp.lang.c++
Subject: Re: conceptual problem with related classes derived in parallel
Message-ID: 
Date: 2 Oct 89 15:30:11 GMT
References: <1071@godot.radonc.unc.edu>
Organization: CMU CS Department
Lines: 46
In-reply-to: sherouse@godot.radonc.unc.edu's message of 27 Sep 89 16:37:11 GMT

George Sherouse writes, paraphrased:

> I want to write a "list" class once, and reuse the code for lists of
> various things. Inheritance doesn't seem to work.  What am I doing wrong?

Congratulations!  You have reached a state of object-oriented
enlightenment in which you realize that inheritance is not the
solution to all software organization problems.  This is a good thing.
Only zealots would claim otherwise.

What you really want here is a "parameterized type."  In some future
version of C++ you will be able to write something like

template  class list {
  protected:
    T* elems;
  public:
    void add_elem(T elem);
}

Which you could then use by simply writing

list or list as a type in your code.  In your example, there was
no obvious reason why "element" and "new_element" were related by
inheritance, other than that you thought they should be in order to
make the list work.  In this scheme, you can leave them as unrelated
types, unless there really is a good reason to derive new_element from
element.

As I said, Parameterized types are a future feature, but for the time
being, you can get the same effect by writing list as a
preprocessor macro (or 2 macros, one for the declaration and one for
the definition).  See Stroustrup p. 210.

Hope this helps.

Dave




--
Dave Detlefs			Any correlation between my employer's opinion
Carnegie-Mellon CS		and my own is statistical rather than causal,
dld@cs.cmu.edu			except in those cases where I have helped to
				form my employer's opinion.  (Null disclaimer.)