From -2151936393208077904
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,ec45d63568ef66b0,start
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1991-04-30 07:03:00 PST
Path: gmdzi!unido!mcsun!uunet!zaphod.mps.ohio-state.edu!rpi!news-server.csri.toronto.edu!utgpu!cunews!csi.uottawa.ca!news
From: hitz@sim5.csi.uottawa.ca (Martin Hitz)
Newsgroups: comp.std.c++
Subject: backward virtual function call
Message-ID: <1991Apr30.024010.4331@csi.uottawa.ca>
Date: 30 Apr 91 02:40:10 GMT
Sender: news@csi.uottawa.ca
Organization: University of Ottawa
Lines: 24
Nntp-Posting-Host: sim5

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)


