Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!odi!valens!dlw From: dlw@odi.com (Dan Weinreb) Newsgroups: comp.lang.c++ Subject: Re: Implementing virtual functions that return reference to self Message-ID: <403@odi.ODI.COM> Date: 27 Jul 89 20:17:37 GMT References: <8975@thorin.cs.unc.edu> Sender: news@odi.com Reply-To: dlw@odi.com Lines: 32 In-reply-to: kelleymt@luther.cs.unc.edu's message of 26 Jul 89 16:13:42 GMT In article <8975@thorin.cs.unc.edu> kelleymt@luther.cs.unc.edu (Michael T. Kelley) writes: In this case, I know I'm dealing with a Derived, so I'd like to be able to use goodbye(). Can someone explain the harm in allowing hello() to return a Derived& in the Derived class? Or is there an implementation issue lurking underneath? My colleagues and I have seen this problem several times, in slightly different guises. As far as we can tell, there is no good solution; you have to use explicit casts. In my experience so far with C++, this is the biggest problem caused by C++'s mixture of run-time and compile-time type checking. (More accurately, its mixture of run-time-polymorphic objects and typed variables.) There's no such problem in languages like Smalltalk-80 and Lisp/CLOS, in which variables are untyped. (I recognize that typed variables have advantages too, and I'm not trying to provoke a general discussion of the virtues and drawbacks of typed variables!) The underlying philosophical problem is that d.hello().goodbye(); is doing something that is perfectly meaningful, but the C++ compile-time type checking system cannot prove in advance that the code meaningful, so the compiler must tag the code as an error. You have to put in an explicit cast into the code in order to assert to C++ that everything's really all legal. I don't see any way to fix this that is consonant with the general C++ language design. It looks like a tough problem. Is there anybody out there who can tell us whether Trellis/Owl has a way to deal with this? Are there other object-oriented languages with typed variables in which an analogous construct can be written? Dan Weinreb Object Design, Inc.