Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!olivea!genie!udel!rochester!kodak!islsun!cok From: cok@islsun.Kodak.COM (David Cok) Newsgroups: comp.lang.c++ Subject: Re: typesafe downward casting Message-ID: <1991Apr9.104918.5353@kodak.kodak.com> Date: 9 Apr 91 10:49:18 GMT References: <27FA1EC4.511C@tct.com> <1991Apr5.150148.21840@kodak.kodak.com> <28007BC8.D71@tct.com> Sender: cok@Kodak.COM Organization: Eastman Kodak Co., Rochester, NY Lines: 36 In article <28007BC8.D71@tct.com> chip@tct.com (Chip Salzenberg) writes: >According to cok@islsun.Kodak.COM (David Cok): .... > >>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. > >That's my most common use. > >>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 (another choice): modify the base class for the added functionality >(add a do-nothing virtual function, etc). That's my choice. >-- You left off the rest of my statement: >> ... or corrupting the base class with the interface to the added >> functionality of the derived class. I just can't see that modifying the base class is a generally reasonable alternative -- even if you do have source code access, which in general you do not. Why should the added functionality of derived classes have to be propagated up the inheritance tree? That seems to violate all principles of modularity in designing classes, causes excessive code bloat and puts all derived class function names in the same name space. David R. Cok Eastman Kodak Company -- Imaging Science Lab cok@Kodak.COM