Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!sdd.hp.com!elroy.jpl.nasa.gov!ames!sgi!dragon!xanadu.wpd.sgi.com!pal From: pal@xanadu.wpd.sgi.com (Anil Pal) Newsgroups: comp.lang.c++ Subject: Re: friend operator +(l,r) vs. operator +(r) Message-ID: <1990Nov26.210244.21500@relay.wpd.sgi.com> Date: 26 Nov 90 21:02:44 GMT References: <11759@hubcap.clemson.edu> Sender: news@relay.wpd.sgi.com ( CNews Account ) Reply-To: pal@sgi.com Organization: Silicon Graphics, Inc. Lines: 25 In article <11759@hubcap.clemson.edu>, grimlok@hubcap.clemson.edu (Mike Percy) writes: |> |> What semantic/operational and/or stylistic differences are there |> between these two definitions? |> |> class foo { |> public: |> foo& operator +(foo& rhs); [... and ...] |> friend foo& operator +(foo& lhs, foo&rhs); |> } 1. The friend operator can perform implicit type conversions on its first argument. Thus if you have a foo(int) constructor, then with the friend operator you can do things like (3 + foovar), where with the member function operator you would have to have (foovar + 3) to get your operator. 2. On the other hand, the member function could be made virtual where the friend function cannot. In a binary operation like +, however, you need to dispatch on both argument types in any case. -- Anil A. Pal, Silicon Graphics, Inc. pal@sgi.com (415)-335-7279