Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!netcom!teda!ditka!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c++ Subject: Re: friend operator +(l,r) vs. operator +(r) Message-ID: <1990Nov30.131808.19717@clear.com> Date: 30 Nov 90 13:18:08 GMT References: <1990Nov21.053431.22340@actrix.co.nz> <1990Nov22.230835.26713@clear.com> Organization: Clear Communications, Inc. Lines: 48 In article Reid Ellis writes: >rmartin@clear.com (Bob Martin) writes: >!foo fa,fb; >!int ia; >!... >!fb=ia+fa; // adding an int to a foo. >! >!If you use the foo::operator+(foo&) method, then the above statement >!will produce a compiler error since foo::operator+(foo&) requires that >!the left hand side of the + operator must be a foo. > >Not necessarily. What if there existed "foo::operator int()" as well >as "foo:foo(int)"? Then the above would work fine. Granted, but then it is not using the foo::operator+(foo&) method. > > Does anyone else think that > > operator+(const type1 &, const type2 &); > > should have a return type of type1 at all times? Although I agree that there is a set of functions f(a,b) which should always return elements of type a, I don't think the rule is global. Furthermore IMHO it should not be applied to an operator which is expected to be commutative (like operator+). Otherwise you are setting up scenarios where a+b != b+a. IMHO this would lead to support nightmares. Consider an assembly language in which you are allowed to do symbolic operations on relocatable (r) and absolute (a) values. In most assemblers r+r is illegal. r+a->r and a+r->r. Which is to say that adding an absolute to a relocatable always yeilds a relocatable. If we were to apply your rule then a+r would have to be illegal since there is no way to return an absolute value in this case. For subtraction: r-a->r a-r is illegal r-r->a. If we were to apply your rule here then there would be no way to subtract two relocatable values (A very important operation!) since the result _must_ be absolute. -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. | Brought to you by Super Global Mega Corp .com