Path: utzoo!mnetor!tmsoft!torsqnt!lethe!yunexus!ists!helios.physics.utoronto.ca!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: asking an object for its type Message-ID: <607@taumet.com> Date: 19 Feb 91 16:07:24 GMT References: <23984@netcom.COM> <1190@sheol.UUCP> <1991Feb19.000449.22255@gpu.utcs.utoronto.ca> Organization: Taumetric Corporation, San Diego Lines: 31 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" >Arg. A totally contextual algorithm. I do NOT want to write a special >function for all shapes to respond to this. What I would really like to >do is something like: > "if x.type=circle then if x.diameter > 30 then return x" >But without a type tag, I can't even check if there IS a diameter! All you have to do is make a virtual function diameter() for all Shapes. 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. If checking diameters of a Shape is something you would expect to do, then Shape should have a virtual function diameter(). There would be 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). 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. -- Steve Clamage, TauMetric Corp, steve@taumet.com