SGI C++ 2.0 Polymorphic Reference Bug
Jeff Carey
jeff at contex.UUCP
Thu Mar 14 03:28:46 AEST 1991
In article <1991Mar12.213359.25893 at odin.corp.sgi.com>, robert at texas.asd.sgi.com (Robert Skinner) writes:
> In article <1688 at contex.UUCP>, jeff at 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 at 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 <iostream.h>
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 at uunet.uu.net
More information about the Comp.sys.sgi
mailing list