Article View: comp.std.c++
Article #919Re: Dynamic type loss considered harmless
From: Chip Salzenberg
Date: Thu, 07 Mar 1991 16:06
Date: Thu, 07 Mar 1991 16:06
28 lines
1928 bytes
1928 bytes
According to thomasw@hpcupt1.cup.hp.com (Thomas Wang):
>Suppose we have an 'array' library that always operates on objects of
>class 'object'. Now suppose you have a class called 'person'. ...
>A 'person*' that was put into the array will come out as 'object*'.
Such an |array| class is badly designed, because it depends on all
objects being derived from |object|, which is an invalid assumption
for the C++ language.
In C++ 2.1, I would make |array| a template, so that the writer of
|array| would not depend on a specific base class like |object|.
Unfortunately, templates are not widely available yet.
Therefore, I use the preprocessor to make type-specific vector classes
from a generic type-ignorant |Vector| class. I use the same approach
for linked lists. These classes work: I use them in production code.
I know that heavy use of the preprocessor is a hack. But, with
apologies to Winston Churchill: `Using the preprocessor for collection
classes is the worst solution imaginable, except for all the others.'
Preprocessor-based collections preserve static type information to
whatever degree is desired by the consumer, which makes them cleaner
than any type-tag solution. And the preprocessor can be ditched later
when templates arrive, without any loss of type safety.
--
Chip Salzenberg at Teltronics/TCT <chip@tct.uucp>, <uunet!pdn!tct!chip>
"All this is conjecture of course, since I *only* post in the nude.
Nothing comes between me and my t.b. Nothing." -- Bill Coderre
Technical Details
Message-ID:
<27D66511.A4A@tct.uucp>Path:
arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!pdn!tscs!tct!chipReferences:
<27CD13B1.649B@tct.uucp> <68950001@hpcupt1.cup.hp.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!pdn!tscs!tct!chip From: chip@tct.uucp (Chip Salzenberg) Newsgroups: comp.std.c++ Subject: Re: Dynamic type loss considered harmless Message-ID: <27D66511.A4A@tct.uucp> Date: 7 Mar 91 16:06:40 GMT References: <27CD13B1.649B@tct.uucp> <68950001@hpcupt1.cup.hp.com> Organization: Teltronics/TCT, Sarasota, FL Lines: 28