Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!cmcl2!rockyd!acf5!sabbagh From: sabbagh@acf5.NYU.EDU (sabbagh) Newsgroups: comp.lang.c++ Subject: Re: asking an object for its type Message-ID: <1485@acf5.NYU.EDU> Date: 21 Feb 91 16:53:24 GMT References: <1991Feb20.232710.7843@ithaca.uucp> Organization: New York University Lines: 60 garry@ithaca.uucp (Garry Wiegand) writes: >This "isKindOf" argument keeps coming up. Perhaps the problem should >be attacked the other way: what would it take to allow an application >to add a virtual function to an existing class (for which the >application programmer doesn't have source)? Unfortunately, much of this discussion has been abstract, in the sense that "C++ should provide the ability to check a class type at run-time". My basic counter-argument is: why? If the application calls for run-time type checking, then it can be built into the system using C++. Its not particularly easy, but it is not wasted effort, SINCE THE DESIGN CALLS FOR THIS ABILITY. This would come up in database applications, graphics, etc. where the end-user needs to select objects at run-time. >A) Allow "change type of". The application can now derive a new class > from each library class and add its virtual function. If the > application does all of the instance-creating, and only creates > derived objects, things work out. *But* if the library creates one > of the instances, the instance's virtual function table won't be the > one the application wants. > So: allow some syntax for an base class instance to be *actually > changed* into a derived class instance. Doesn't work if the derived > class requires extra instance variables (can't change the size > without considerable trickiness). *Does* work (the v-table pointer > is switched) for extra virtual functions. > (The symmetric operation (derived class to actual base class) should > also be legal, but right now I don't see the utility.) > This suggestion is already in C++; its called "inheritance". The client programmer (person who is using the library) call inherit from the supplied classes, providing whatever additional virtual functions s/he likes. I agree, however, that it is hard to decide which member functions to make virtual; but this too can be worked around. Finally, general observations. C++ was originally designed to augment the C with object-oriented capabilities and other facilities to make programming-in-the-large easier. C was intended as a systems language; C++ still hold true to this. If you are looking for a fully interpreted environment in which everything is an object, use Smalltalk. If you want to build applications on a wide variety of hardware and software platforms and want better productivity than offered by C, use C++. You can implement Smalltalk (or CLOS, or whatever) in C++, but then you would be implementing an intrepreter, not an easy task in any language. Hadil G. Sabbagh E-mail: sabbagh@cs.nyu.edu Voice: (212) 998-3125 Snail: Courant Institute of Math. Sci. 251 Mercer St. New York,NY 10012 "Injustice anywhere is a threat to justice everywhere." - Martin Luther King, Jr. Disclaimer: This is not a disclaimer.