Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: more on "foo & operator+ (const foo&)" Message-ID: <59617@microsoft.UUCP> Date: 6 Dec 90 20:18:43 GMT References: <11759@hubcap.clemson.edu| <1939@m1.cs.man.ac.uk> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 33 In article writes: |Only if your operator+() assumed that none of the parameters could |ever be the result of a previous addition. I agree that this |technique should be avoided if possible, but if you need the speed, |you can do it if you're careful. e.g., for an integer class "Tint": | |Tint& operator+(const Tint &left, const Tint &right) |{ | static Tint stat; | | stat.value = left.value + right.value; | return stat; |} | |This will never have a problem with either of 'l' or 'r' being the |result of a previous '+' -- i.e., a reference to 's'. | |But I should state again that I agree that this should be avoided if |possible. Be sure that doing this will really speed up your code by |profiling it before introducing something that could cause subtle |errors. ??? I guess I don't understand what you're suggesting. The above example has [IMHO] unsubtle problems, consider: Tint a=100, b=200, c=300, d=400, e; e = (a + b) + (c + d); e.print(); results in 600 on my compiler [your compiler might give a different answer -- but its unlikely to be the 'right' one!] Or is this what you mean by "subtle errors" ? Brought to you by Super Global Mega Corp .com