Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!portal!fernwood!lia!jgro From: jgro@lia (Jeremy Grodberg) Newsgroups: comp.lang.c++ Subject: Re: asking an object for its type Message-ID: <1991Feb20.025745.2970@lia> Date: 20 Feb 91 02:57:45 GMT References: <23984@netcom.COM> <1190@sheol.UUCP> <1991Feb19.000449.22255@gpu.utcs.utoronto.ca> <1991Feb19.023528.29494@Think.COM> Reply-To: jgro@lia.com (Jeremy Grodberg) Lines: 26 In article <1991Feb19.023528.29494@Think.COM> barmar@think.com (Barry Margolin) writes: >In article <1991Feb19.000449.22255@gpu.utcs.utoronto.ca> craig@gpu.utcs.utoronto.ca (Craig Hubley) writes: >>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" > >Even in languages that provide a way to do that, you probably wouldn't want >to write it that way, because it doesn't handle derived classes properly. >For instance, if square is derived from regular_polygon and quadrilateral, >x.type can't be all three. What I would like to see is something like Object Pascal's member() function. It takes an object and a type name, and returns true if the object is of that type (or is derived from that type). Of course, member is a bad choice of names for C++, so I'll just call the function "is." So you would write Craig's query: "if (is(x, Circle)) then if x.diameter > 30 then return x" All this information is available at compile time, the problem, as pointed out in the ARM, is agreeing what information to store and how and where to store it. I'm all for it. -- Jeremy Grodberg "I don't feel witty today. Don't bug me." jgro@lia.com