Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!udel!rochester!kodak!islsun!cok From: cok@islsun.Kodak.COM (David Cok) Newsgroups: comp.lang.c++ Subject: Re: typesafe downward casting Message-ID: <1991Apr5.150148.21840@kodak.kodak.com> Date: 5 Apr 91 15:01:48 GMT References: <18.UUL1.3#8618@softrue.UUCP> <1991Apr2.161809.16952@kodak.kodak.com> <27FA1EC4.511C@tct.com> Sender: cok@Kodak.COM Organization: Eastman Kodak Co., Rochester, NY Lines: 34 In article <27FA1EC4.511C@tct.com> chip@tct.com (Chip Salzenberg) writes: >According to cok@islsun.Kodak.COM (David Cok): >>I would maintain ... that a good program generally has few casts and a >>good programming language allows most programs to be written without casts. > >Indeed. And that is true of C++ today -- IF you are willing to avoid >using Base* all over the place when Derived* is meant. (Template >collections are a tremendous help.) > >In other words, the way to regain an object's lost static type is not >to lose it in the first place. >-- I am certainly looking forward to having an environment that supports templates (no such luck here yet), and think it will do great things for reusing implementations of Lists, etc., but I don't think it is a replacement for inheritance. If you use inheritance simply to provide a new implementation of a class, you can keep manipulating the Base* values, and never need to deal with the Derived class at all except to state in a declaration which derived class to use, e.g. List* list = new MySpecialListImplementation. But another use of inheritance is to add functionality to an existing class by deriving from it. Now virtual functions which returned Base* will still return Base* in the derived class -- so I cannot help losing the object's static type. In this context one must either have contravariance on the return type of virtual functions or type-safe down casting -- or resort to current C++ which provides only type-unsafe down casting or corrupting the base class with the interface to the added functionality of the derived class. David R. Cok Eastman Kodak Company -- Imaging Science Lab cok@Kodak.COM