Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!tut.cis.ohio-state.edu!ucbvax!lexvmc.iinus1.ibm.com!schweitz From: schweitz@lexvmc.iinus1.ibm.com ("Eric Schweitz") Newsgroups: comp.lang.c++ Subject: re: C++ typing not so strong Message-ID: <9102182022.AA05904@ucbvax.Berkeley.EDU> Date: 18 Feb 91 20:23:57 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 70 | David R. Cok | Eastman Kodak Company | cok@Kodak.COM | 716-477-7086 writes: | We had a base class Image from which some derived classes DoubleImage, | FloatImage, IntImage etc. were derived. We defined lots of relevant | functions including (for example) operator- . | | ... we defined member functions of the form | | const DoubleImage& DoubleImage::operator-() const; | const FloatImage& FloatImage::operator-() const; | | But we also want some "virtualness" here. Perhaps you could tell us why you `needed' these member functions to be virtual. | Example A: | | class Image { | public: | virtual const Image& operator-() const = 0; | }; | | class DoubleImage: public Image { | public: | const DoubleImage& operator-() const; | }; | | BUT THIS IS ILLEGAL because the two operator- declarations do not return the | same type. You're right here. However, if ``operator-'' had been declared as a non-`pure virtual' member function, you would have simple overloading of the unary ``-'' operator. | Question 1: Does anyone have a better suggestion? Yeah, don't make the operator- a pure virtual function if you don't have to. | Question 2: What is the rationale for requiring that the implementations of | virtual functions in derived classes return exactly the same type as is | declared in the base class? The rationale behind this is that it would make the following possible : //.... Image i, *ip; DoubleImage *dp; ip = &i; dp = func ((DoubleImage*) ip); //.... where, func () is: //.... DoubleImage* func (DoubleImage* p) { return p->operator-(); } //.... hence, func() would return a Image* to dp which is expecting a DoubleImage*. Schweitz. schweitz@lexvmc.iinus1.ibm.com