Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!rpi!crdgw1!sunne!brownpc From: brownpc@sunne.crd.ge.com (Paul C Brown) Newsgroups: comp.std.c++ Subject: Re: the most dissatisfying part of c++ Message-ID: <10656@crdgw1.crd.ge.com> Date: 1 Aug 90 21:02:00 GMT References: <27634@netnews.upenn.edu> Sender: news@crdgw1.crd.ge.com Reply-To: brownpc@sunne.crd.ge.com (Paul C Brown) Lines: 58 In article <27634@netnews.upenn.edu>, limsoon@saul.cis.upenn.edu (Limsoon Wong) writes: |> |>the program below is a contrived one. it cannot be compiled. |>the reason is a type fault. however, it exhibits a very central |>characteristic of update. something should be done to allow |>this kinds of programs. ---limsoon. |> |> |> |>#include |>class a { |> public: |> a& test() { |> // do some modification to the state of this object, |> // then ... |> return *this; |>}; |>class b : public a { |> public: |> int b; |>}; |>main() { |> b B; |> B = B.test(); |> // type violation despite the fact that |> // `B' and `B.test()' are the same object. |>} Your proposed program is not "type" correct. The method "test" returns an object of type a, not of type b. Objects of type a do not exhibit all of the properties of type b, and therefore are not valid values for variables of type b. The following version of main is type correct: main() { b B; a A; A = B.test(); }; To accomplish what you were trying to do would require a second method on class b, also named test, that returns an object of type b. If the original method on class a does, indeed, simply modify the referenced instance, then the new method could be as simple as: b::test() { a temp; temp = this.test(); return *this; }; As you have observed, strong typing does not give you everything for free! Paul C. Brown brownpc@crd.ge.com GE Corporate Research & Development Schenectady, New York