Article View: comp.std.c++
Article #1063Re: backward virtual function call
From: Andrew Koenig
Date: Tue, 30 Apr 1991 21:15
Date: Tue, 30 Apr 1991 21:15
31 lines
1081 bytes
1081 bytes
In article <1991Apr30.024010.4331@csi.uottawa.ca> hitz@sim5.csi.uottawa.ca (Martin Hitz) writes:
> #include <stream.h>
> struct X {
> virtual f() { return 1; }
> };
> struct Y : X {
> f() { return 2; }
> };
> main()
> {
> X x;
> Y * y = (Y *) &x;
> cout << y->f();
> }
The effects of things like this are undefined.
In this particular example, I expect that many implementations
will print 2, but the assignment
Y * y = (Y *) &x;
is well-defined only if &x points at an object of class Y --
which, of course, it does not.
--
--Andrew Koenig
ark@europa.att.com
Technical Details
Message-ID:
<20292@alice.att.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!andante!alice!arkReferences:
<1991Apr30.024010.4331@csi.uottawa.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!andante!alice!ark From: ark@alice.att.com (Andrew Koenig) Newsgroups: comp.std.c++ Subject: Re: backward virtual function call Message-ID: <20292@alice.att.com> Date: 30 Apr 91 21:15:21 GMT References: <1991Apr30.024010.4331@csi.uottawa.ca> Reply-To: ark@alice.UUCP () Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 31