Path: utzoo!mnetor!tmsoft!torsqnt!geac!alias!barney!rae From: rae@gpu.utcs.toronto.edu (Reid Ellis) Newsgroups: comp.sys.mac.programmer Subject: Re: C++ CString sample code Message-ID: Date: 27 Jan 91 08:23:23 GMT References: <5672@rex.cs.tulane.edu> <11830@goofy.Apple.COM> Sender: Reid Ellis Reply-To: Reid Ellis Organization: Alias Research, Inc. Toronto ON Canada Lines: 33 I asked: >Why is it > void operator +=( const CString& s1); >and not > CString& operator +=( const CString& s1); >?? Jeff E Mandel writes: >The answer is subtle. An operator has access to the object which invokes >it. The operator simply modifies the existing object, rather than >returning a new one. But I have it returning a reference [CString & operator+=] not a new object [CString operator+=]. Larry Rosenstein writes: >.. you would have operator+= return a const CString&. It shouldn't be const. If you are using operator+=() as defined above, you do not have a const object, and a method shouldn't "make" it const for no good reason. e.g., defining operator+=() as returning a "const CString &" would make the following illegal: if((cstr += otherCstr)[12] == '\n') ... Admittedly, using the result of += in such a non-const manner is unusual, but I see no reason to forbid it.. Reid -- Reid Ellis 176 Brookbanks Drive, Toronto ON, M3A 2T5 Canada rae@gpu.utcs.toronto.edu || rae%alias@csri.toronto.edu CDA0610@applelink.apple.com || +1 416 446 1644