Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!usc!apple!agate!pasteur!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: asking an object for its type Message-ID: <11284@pasteur.Berkeley.EDU> Date: 20 Feb 91 01:37:54 GMT References: <23984@netcom.COM> <1190@sheol.UUCP> <1991Feb19.000449.22255@gpu.utcs.utoronto.ca> <607@taumet.com> <65451@brunix.UUCP> Sender: news@pasteur.Berkeley.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 33 In article <65451@brunix.UUCP>, sdm@cs.brown.edu (Scott Meyers) writes: |> The bottom line is that virtual functions are wonderful things and can |> almost always be used to achieve what you want, but there are times when |> you truly *do* need to get type information as you run. Scott then goes on to talk about how the ANSI committee is unlikely to give him what he wants. What do you care what the committee says? If you must have this feature, write, in the baseclass virtual const char* myType() = 0; then do const char* ControlArc::myType() { return "ControlArc";} It is already in the language if you must have it. However, I would still discourage you from this type of programming. Why? Because people will add new types of Arcs and you won't deal with them properly. You're dealing with objects based on an exact match of some type string instead of based on some relevant attribute that a virtual function could return. You're wiring in a list of classes you can handle, requiring more code rewriting as classes are added. But if you really want to do it, you can, so stop saying that C++ won't give you run-time type information. It's trivial to add if you want it. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck