Path: utzoo!news-server.csri.toronto.edu!rutgers!dimacs.rutgers.edu!seismo!uunet!contex!jeff From: jeff@contex.UUCP (Jeff Carey) Newsgroups: comp.sys.sgi Subject: Re: SGI C++ 2.0 Polymorphic Reference Bug Summary: then, this must be a bug... Keywords: SGI PI C++ 2.0 polymophism reference bug Message-ID: <1692@contex.UUCP> Date: 13 Mar 91 17:28:46 GMT References: <1688@contex.UUCP> <1991Mar12.213359.25893@odin.corp.sgi.com> Organization: Xyvision Design Systems, Wakefield MA Lines: 86 In article <1991Mar12.213359.25893@odin.corp.sgi.com>, robert@texas.asd.sgi.com (Robert Skinner) writes: > In article <1688@contex.UUCP>, jeff@contex.UUCP (Jeff Carey) writes: > |> > |> (deleted text) > |> > |> of _The_Annotated_C++_Reference_Manual_ and found the following bug. You'll > |> notice that the bug is in the call made to f() from the MW object reference > |> (MW&). > |> > |> (deleted code, results, and more text) > |> > |> This is a bug isn't it...polymorphism of this sort should be supported by > |> references as well as pointers, right?! > > no, its not a bug. I'll let one of our compiler wizards explain the > details, but here's my rationalization: > > You've declared rmw to be a MW, and the compiler treats it as one, > calling MW::f() directly. (I think of the '&' operator as a > convenience. I really don't know what effect it has here.) > Polymorphism is only supported for pointers, where the compiler doesn't > really know what subclass the pointer really points at. > > -- > Robert Skinner > robert@sgi.com > Robert- If this is not a bug, and "polymorphism is only supported for pointers," then the call to the additional g() functions in the following code will illustrate what must certainly be a bug: ------------------ beginning of enlarged test.c++ code ----------------------- #include class W { public: virtual void f() { cout << "W::f\n"; } }; class MW : public virtual W { public: virtual void g() { cout << "MW::g\n"; } }; class BW : public virtual W { public: void f() { cout << "BW::f\n"; } }; class BMW : public BW, public MW { public: void g() { cout << "BMW::g\n"; } }; main() { BMW bmw; MW* pmw = &bmw; pmw->f(); pmw->g(); MW& rmw = bmw; rmw.f(); rmw.g(); return(0); } -------------------------- end of enlarged test code ------------------------- With these results: $ test BW::f BMW::g W::f BMW::g Based on your assertion, I would have predicted that the two lines of diagnostics related to the g() call would have been different; the first is OK, but the second would have to be "MW::g". Polymorphism appears to be currently supported in this context by references. Something must be wrong --- And just trying to comprehend --- Thanks for helping --- I'm sure this won't be the end --- jeff -- ------------------------ Jeff Carey contex!jeff@uunet.uu.net