Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!think.com!zaphod.mps.ohio-state.edu!usc!apple!lsr From: lsr@Apple.com (Larry Rosenstein) Newsgroups: comp.sys.mac.programmer Subject: Re: C++ CString sample code Message-ID: <11830@goofy.Apple.COM> Date: 24 Jan 91 01:56:18 GMT References: <5672@rex.cs.tulane.edu> Sender: usenet@Apple.COM Organization: Apple Computer, Inc. Lines: 26 In article <5672@rex.cs.tulane.edu>, mandel@vax.anes.tulane.edu (Jeff E Mandel MD MS) writes: > > From: Reid Ellis > > >Why is it > > void operator +=( const CString& s1); > >and not > > CString& operator +=( const CString& s1); > >?? > > > > Reid > > 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. Thus: You could write operator+= either way, depending on what behavior you wanted to provide. For example, for built-in types, you can write something like x = (y += z), which adds z to y and assigns the result to x. If CString::operator+= returns void, then this kinds of statement is illegal. To allow it, you would have operator+= return a const CString&.