Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: Can I make overloaded operator functions virtual? Message-ID: <1991Mar22.044801.26365@world.std.com> Date: 22 Mar 91 04:48:01 GMT References: <1991Mar20.074256.334@csis.dit.csiro.au> <1991Mar21.184133.14506@watson.ibm.com> Distribution: usa Organization: The World Public Access UNIX, Brookline, MA Lines: 36 mittle@blinn.watson.ibm.com (Josh Mittleman) writes: > class Base > { > public: > virtual int operator==(const Base&); > }; > > class Derived : public Base > { > public: > int operator==(const Derived&); > }; > > We would really like Derived::operator== to match the virtual > Base::operator==, but we need to be able to treat the argument as a > Derived&. No, you really *don't* want that. Allowing something like that would open a major hole in the type system. Assuming Derived::operator==() actually did override Base::operator==(), code like the following would be possible: Base b; Derived d; Base& br = d; if (br == b) ... Since "br" actually refers to a Derived object, the "==" invokes Derived::operator==() -- but it passes a Base object as the argument instead of the Derived object expected by Derived::operator==(). Boom! This is a frequent request, but it just doesn't make sense when you look at the ramifications. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com