Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!boulder!sunybcs!rutgers!att!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: Overloading virtual functions with a different return type Summary: Types of virtual functions must match exactly Message-ID: <8938@alice.UUCP> Date: 18 Feb 89 20:17:14 GMT References: <11034@tekecs.TEK.COM> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 42 In article <11034@tekecs.TEK.COM>, brucec@demiurge.gwd.tek.com (Bruce Cohen) writes: > ... > > struct Base > { > virtual void* Obj() { return obj; } > void* obj; > }; > struct Derived : public Base > { > virtual int Obj() { return (int)obj; } > }; > > gives me the following message: > > error: virtual Derived::Obj() type mismatch: int Derived::() and void *Base::() > > Question 1: is this really a language issue, or just a quirk of the current > version of cfront? This is really a language issue. At the point of call of a virtual function the code doesn't know what function is going to be called. Thus the calling sequence and arrangements for receipt of the return value must be identical for each instance of any virtual function, and the only way to guarantee that is to require that they have the same types. For example, suppose your example were allowed and then you tried to port the program to a machine that returned integers in one register and pointers in another. Disaster would likely occur. > Question 2: can anyone suggest a way around this? Try to avoid using base classes as unions. The macro technique for container classes works, if your cpp is robust. - - - - - - - -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229