Path: utzoo!attcan!uunet!decwrl!sgi!shinobu!odin!thebeach.wpd.sgi.com!shap From: shap@thebeach.wpd.sgi.com (Jonathan Shapiro) Newsgroups: comp.std.c++ Subject: Re: the most dissatisfying part of c++ Message-ID: <11296@odin.corp.sgi.com> Date: 2 Aug 90 18:17:18 GMT References: <27634@netnews.upenn.edu> Sender: news@odin.corp.sgi.com Reply-To: shap@sgi.com Organization: Silicon Graphics, WorkGroup Products Division Lines: 37 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. > } This has surprising consequences in the face of virtuals, since the caller thinks they are getting an instance of an A, not a B. From a static typechecking angle it works, and the parallel argument for returning pointer to derived in place of pointer to base follows. However, in the face of multiple inheritance this doesn't work at all. Jon