Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!seismo!elsie!nih-csl!keith From: keith@nih-csl.UUCP Newsgroups: comp.lang.c++ Subject: Re: Questions about C++ Message-ID: <226@nih-csl.UUCP> Date: Fri, 19-Jun-87 14:40:18 EDT Article-I.D.: nih-csl.226 Posted: Fri Jun 19 14:40:18 1987 Date-Received: Sat, 20-Jun-87 10:04:12 EDT References: <964@vaxb.calgary.UUCP> <129@otc.OZ> Distribution: comp Organization: NIH-CSL, Bethesda, MD Lines: 62 Summary: Situation justifying use of "in" described In article <129@otc.OZ>, mikem@otc.OZ (Mike Mowbray) writes: > In article <964@vaxb.calgary.UUCP>, west@calgary.UUCP (Darrin West) says: > > >> > o Associated operators like 'in' and 'qua' [ see below ] aren't available > >> > >> These are not really needed. You achieve the same result in other ways. See > >> below. > > > > I don't think you can make this blanket statement. There are a lot of > > simulationists who would not like to live without it. :-) ? > > Earlier in my C++ "career", I wondered about this very thing, thinking > it would be nice to know at run-time if an instance of class x was > actually a class something_else. The thing that leads me to make the above > "blanket statement" is that operators like in and qua are basically only > needed is to enable you to *do* things based on what they return. > The Simula "in" operator is, I believe, like Smalltalk's "isKindOf" method, i.e, it allows you to test an object to see if it is an instance of a particular class or of a derived class. IsKindOf is useful for checking the type of argument objects to virtual functions, even in a statically type checked language like C++. Suppose you're implementing a Smalltalk-like class Dictionary. Class Dictionary is a derived class of Collection, which in turn is derived from class Object: class Object { ... }; class Collection : public Object { ... virtual void add(const Object&); // add an Object to a Collection ... }; class Dictionary : public Collection { ... virtual void add(const Object&); // add an Association to a Dictionary ... }; The problem is that class Dictionary should only accept instances of class Association (which is a key object - value object pair) or of a class derived from Association as arguments, so you should check the class of the argument. You can't do this just by defining Dictionary::add(const Association&), because you can't overload a virtual function in a derived class without also overloading it in the base class. This means that to use static type checking you'd have to also define Collection::add(const Association&), which is undesirable because you should be able to define new kinds of Collections without having to modify class Collection itself. So to make Dictionary::add check its argument class you need to do it at run-time with isKindOf (a.k.a. "in"). -- Keith Gorlen phone: (301) 496-5363 Building 12A, Room 2017 uucp: seismo!elsie!nih-csl!keith National Institutes of Health Bethesda, MD 20892