Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: Arguments to Overloaded Operators Message-ID: <6590145@hplsla.HP.COM> Date: 9 Jun 89 01:12:12 GMT References: <11032@orstcs.CS.ORST.EDU> Organization: HP Lake Stevens, WA Lines: 49 // Is anybody other than myself bothered by the fact that in an overloaded // operator the treatment of the left and right arguments is not consistent? // In particular, the class to examine is found, naturally, by examining the // dynamic (run time) type of the left argument, whereas the disambiguation // of the overloaded operator is found using the static (declared) type of the // right argument. //Don't worry, be happy. //To illustrate, consider the following simple program: #include class Bar; class Foo { public: virtual int reverseAdd(Foo& x) { cout << "in Foo,Foo + Foo\n"; return 7;} virtual int reverseAdd(Bar& x) { cout << "in Foo,Bar + Foo\n"; return 7;} virtual int operator+(Foo& x) { return x.reverseAdd(*this);} virtual int operator+(Bar& x) ; }; class Bar : public Foo { public: int reverseAdd(Foo& x) { cout << "in Bar, Foo + Bar\n"; return 7;} int reverseAdd(Bar& x) { cout << "in Bar, Bar + Bar\n"; return 7;} int operator+(Foo& x) { return x.reverseAdd(*this);} int operator+(Bar& x) { return x.reverseAdd(*this);} }; inline int Foo::operator+(Bar& x) { return x.reverseAdd(*this);} main() { Foo *a; a = new Bar(); (*a) + (*a); } // of course, if (a+b) == (b+a) in whatever system of math, // then you needn't reverse the order of the adds... // [...these examples aren't *really* in-line, of course...]