Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!samsung!umich!vela!schemers From: schemers@vela.acs.oakland.edu (Roland Schemers III) Newsgroups: comp.std.c++ Subject: Re: the most dissatisfying part of c++ Message-ID: <2314@vela.acs.oakland.edu> Date: 31 Jul 90 18:18:10 GMT References: <27634@netnews.upenn.edu> Reply-To: schemers@vela.acs.oakland.edu (Roland Schemers III) Organization: Oakland University, Rochester MI Lines: 66 In article <27634@netnews.upenn.edu> limsoon@saul.cis.upenn.edu.UUCP (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. >} I for one do not see where 'B' and 'B.test()' are the same object. The function B.test() clearly returns a reference to an 'a' object. What you could do is: class b : public a { public: int b; b &test() { a::test(); return *this; } }; (Forgive me if the syntax is not correct) I understand your point of view though, and I think it would be nice if there was some construct in C++ to do what you want. I know some of my classes could use it. Maybe something like: class a { public: virtual& test() { // do some modification to the state of this object, // then ... return *this; }; In otherwords, make the return type 'virtual' like functions. In this case, the virtual means the type of the class. So when class 'b' is derived from class 'a', all virtual return types in class 'a' will now return type 'b'. Of course this we be very ugly to implement, and is sort of like parametric return types. Actually, would templates solve this problem? Roland -- Roland J. Schemers III Systems Programmer schemers@vela.acs.oakland.edu (Ultrix) Oakland University schemers@argo.acs.oakland.edu (VMS) Rochester, MI 48309-4401 "Get off your LEF and do something!" (313)-370-4323