Xref: utzoo comp.object:578 comp.lang.c++:5792 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!clyde!uunet!snorkelwacker!apple!vsi1!octopus!sjsumcs!33014-18 From: 33014-18@sjsumcs.sjsu.edu (Eduardo Horvath) Newsgroups: comp.object,comp.lang.c++ Subject: Re: (Very) Dymanic Typing (Was: Dumb question) Message-ID: <1989Dec5.195643.13592@sjsumcs.sjsu.edu> Date: 5 Dec 89 19:56:43 GMT References: <61737@aerospace.AERO.ORG> <9938@june.cs.washington.edu> <2122@tukki.jyu.fi> <2159@tukki.jyu.fi> <9191@microsoft.UUCP> <32106@watmath.waterloo.edu> <62298@aerospace.AERO.ORG> Reply-To: 33014-18@sjsumcs.SJSU.EDU (Eduardo Horvath) Organization: San Jose State University Lines: 70 In article <62298@aerospace.AERO.ORG> abbott@aero.UUCP (Russell J. Abbott) writes: >In article <32106@watmath.waterloo.edu> gjditchfield@watmsg.waterloo.edu (Glen Ditchfield) writes: >> ... In practice, in C++, wouldn't it be simpler to pass a comparison >>function as an argument to the sort() function? > >Can you really do that in C++? What would the declarations look like? >That would be even more flexible than the generic sort that I asked >about originally. > This is an old trick from C. You pass a pointer to a function as an argument to a sort function, and the sort function calls the comparison. A standard library function is qsort which works something like this: int comp(item* item1,item* item2) { return *item1-*item2; } void qsort(item *array,int (*func)()) { do stuff... if( func(&item[i],&item[j] ) swap_items(); .... } main() { item stuff[100]; qsort(stuff,comp); // 'comp' is a pointer to the entry of 'comp()' } This is somewhat simplified because qsort() will work on an array of *ANYTHING* if the comparator is written correctly. This generality makes coding qsort and comp somewhat difficult, but is the ultimate in re-usable code (no re-compiling of qsort is necessary.) >In Smalltalk one can ask an object to "perform" a "message." (I gather >that in C++ terminology a "message" is a call, along with its >argument(s), to a "member" function. Again, pardon my lack of >familiarity with C++.) Smalltalk's "perform" is similar to "eval" in >Lisp in that the thing to be perform'ed or eval'ed is unknown at program >entry/compilation time. In such a situation there is *no* type >information at all available to the compiler/language processor. So how >does a statically typed language like C++ deal with that? And if it >does deal with it: (a) is the static type checking that it does worth >bothing with since there seems to be nothing that can be statically >checked and (b) doesn't there have to be dynamic type checking of the >same order of complexity as goes on in Smalltalk anyway? >-- >-- Russ abbott@itro3.aero.org Type checking is all done statically. Types do not change. The only run-time problems possible with this approach are: a) Passing an invalid pointer or invalid array offset. This should be either impossible, or checked in code before it is used, depending on design. Bugs do, of course, occur. b) Linkage problems occuring from compiling different object modules with different function declarations. This was a problem with classic C, but in ANSI C or C++, correct prototyping and recompiling all files where prototypes have been changed will usually solve this problem. I hope this helps. Eduardo Horvath | San Jose State | 33014-18@sjsumcs.SJSU.EDU