Path: 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 In article <1991Apr30.024010.4331@csi.uottawa.ca> hitz@sim5.csi.uottawa.ca (Martin Hitz) writes: > #include > 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