Article View: comp.std.c++
Article #1058backward virtual function call
From: Martin Hitz
Date: Tue, 30 Apr 1991 02:40
Date: Tue, 30 Apr 1991 02:40
24 lines
884 bytes
884 bytes
I wonder if the following is guaranteed to print 1:
#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 ARM explains the usual case, that a call of f() for an object of
class Y invokes Y::f(), even if it is called via a pointer to X,
but I couldn't find anything about the above case. Zortech and g++
both yield 1 for this example. Is this considered to be the standard
behaviour?
Martin Hitz (hitz@csi.uottawa.ca)
Technical Details
Message-ID:
<1991Apr30.024010.4331@csi.uottawa.ca>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!cunews!csi.uottawa.ca!newsShow Full Headers
Complete Article Headers
Newsgroups: comp.std.c++ Path: arc1-ca.pugleaf.net!utzoo!utgpu!cunews!csi.uottawa.ca!news From: hitz@sim5.csi.uottawa.ca (Martin Hitz) Subject: backward virtual function call Message-ID: <1991Apr30.024010.4331@csi.uottawa.ca> Sender: news@csi.uottawa.ca Nntp-Posting-Host: sim5 Organization: University of Ottawa Date: Tue, 30 Apr 91 02:40:10 GMT