Newsgroups: comp.lang.c++
Path: utzoo!utgpu!craig
From: craig@gpu.utcs.utoronto.ca (Craig Hubley)
Subject: Re: asking an object for its type
Message-ID: <1991Feb22.195826.14008@gpu.utcs.utoronto.ca>
Organization: UTCS Public Access
References: <23984@netcom.COM> <1190@sheol.UUCP> <1991Feb19.000449.22255@gpu.utcs.utoronto.ca> <607@taumet.com>
Date: Fri, 22 Feb 1991 19:58:26 GMT

In article <607@taumet.com> steve@taumet.com (Stephen Clamage) writes:
>craig@gpu.utcs.utoronto.ca (Craig Hubley) writes:
>>
>>imagine a database query:
>>
>>	"from the list of shapes, show me all the circles of diameter > 30"
>
>All you have to do is make a virtual function diameter() for all Shapes.

This solution has been rejected already by several people here, so I
won't repeat the reasons except to say that I don't own Shape but own
several of its subclassees.

>A function which expects to operate on a class of type Shape may
>reaonably expect all the functionality of Shape to be present.  It may
>not reasonably expect specific behaviors of classes derived from Shape
>to be present.

Part of being a "pointer to Shape" is the ability to point to subclasses.
All virtual functions guarantee is to support the same interface, not
the same behavior.  Clearly quite different things will happen when
different shapes are "stretched", for instance.

>If checking diameters of a Shape is something you would expect to do,
>then Shape should have a virtual function diameter().  There would be

Absolutely not.  Shapes in general do not have diameters.  

>defined behavior (such as return 0 or throw an exception) for classes
>which do not have diameters (i.e., a Shape which is not closed).

And I do not want to create a type bureacracy problem (i.e. reorganizing all 
the types) every single time I have a small or incidental need to distinguish
something.  This is a very large solution to a very small problem, as is
adopting an entire base class library, as some others have suggested doing.

>But if from class Shape you derive ClosedShape and OpenShape, and have
>a diameter only for a ClosedShape, you have no business looking for a
>diameter on a list of Shapes -- only on a list of ClosedShapes.  This
>is all part of the system design.

I don't need a lecture on OO design.  :)  I've been doing it since early '86,
and C++ is the first language I've seen that enforces this approach
dogmatically and refuses to acknowledge the obvious situations where it is
preferable to test for type than reorganize the entire type matrix.  It is
a mistake, and even Stroustrop and Koenig apparently acknowledge that
something must be done about it (I'm told they prefer the conditional-cast 
solution)

>Steve Clamage, TauMetric Corp, steve@taumet.com


-- 
  Craig Hubley   "...get rid of a man as soon as he thinks himself an expert."
  Craig Hubley & Associates------------------------------------Henry Ford Sr.
  craig@gpu.utcs.Utoronto.CA   UUNET!utai!utgpu!craig   craig@utorgpu.BITNET
  craig@gpu.utcs.toronto.EDU   {allegra,bnr-vpa,decvax}!utcsri!utgpu!craig
