Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!orstcs!mist!budd From: budd@mist.CS.ORST.EDU (Tim Budd) Newsgroups: comp.lang.c++ Subject: Arguments to Overloaded Operators Message-ID: <11032@orstcs.CS.ORST.EDU> Date: 7 Jun 89 21:24:56 GMT Sender: usenet@orstcs.CS.ORST.EDU Lines: 38 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. To illustrate, consider the following simple program. Although a is declared to be of type Foo (the static type) it actually contains a value of the derived type Bar (the dynamic type). When a is added to itself the method is found in class Bar (the dynamic type), but the version selected depends only on the declaration for a (the static type) and not its current value. This ``Bar,Foo'' is printed, not ``Bar,Bar''. --tim budd (budd@cs.orst.edu) class Bar; class Foo { public: virtual int operator+(Foo& x) { cout << "in Foo,Foo + \n"; return 7;} virtual int operator+(Bar& x) { cout << "in Foo,Bar + \n"; return 7;} }; class Bar : public Foo { public: int operator+(Foo& x) { cout << "in Bar,Foo + \n"; return 7;} int operator+(Bar& x) { cout << "in Bar,Bar + \n"; return 7;} }; main() { Foo *a; a = new Bar(); (*a) + (*a); }