Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!ut-emx!nowhere!sking From: sking@nowhere.uucp (Steven King) Newsgroups: comp.lang.c++ Subject: Re: friend operator +(l,r) vs. operator +(r) Message-ID: <1990Nov29.023619.29838@nowhere.uucp> Date: 29 Nov 90 02:36:19 GMT Organization: nowhere Lines: 53 Various people have responded to various aspects of the original post; I think it is worth pointing out that under Cfront, for binary operators returning an instance of a class, one can put the body of the actual function in the corresponding assignment operator and define the binary operator as an inline that creates a temporary and assigns it the value of one of the binary args, then applies the assignment operator against the temporary and the remaining binary arg, returning the temporary. This DOES NOT result in a dangling reference, even if the translator can not inline the expression. ie., class point { double x,y,z ; public: point ( point & ) ; void operator += ( point & ) ; inline friend point &operator + ( point &, point & ); } ; inline point &operator + ( point &a1, point &a2 ) { point a0 = a1 ; a0 += a2 ; return a0 ; } This works, even in expressions like point p1, p2, p3, p4 ; p1 = p2 + p3 + p4 ; because the temporaries are created at the scope of the invoking expression. It will work even though the translator may not be able to inline the second invocation of the operator + ; it will create a static function taking three arguements, one of which is the temporary result. The biggest problem is that a lot of temporaries can be created, often without the programmer being aware of it, and the complexity may overwhelm less robust C compilers. CAVEAT This behavior is easily demonstrated for Cfront 2.0, however I could not find anything in Lippman, E&S, or the Cfront manual to indicate if was one of those peculuarities specific to Cfront or part of the language. -- sking@nowhere | better to remain unseen ..!cs.utexas.edu!ut-emx!nowhere!sking | and be thought a fool | than to post | and remove all doubt...