Xref: utzoo comp.lang.c++:4278 gnu.g++:326 Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!sun-barr!newstop!sun!illini!acw From: acw%illini@Sun.COM (Alex Wu [GPD] x6-6874) Newsgroups: comp.lang.c++,gnu.g++ Subject: Re: What's the diff? Message-ID: <119611@sun.Eng.Sun.COM> Date: 4 Aug 89 21:34:29 GMT References: <2731@hsinchu.sw.mcc.com> Sender: news@sun.Eng.Sun.COM Lines: 38 In article <2731@hsinchu.sw.mcc.com>, knutson@hsinchu.sw.mcc.com (Jim Knutson) writes: > What's the difference between the ++ operator overload functions in > these two examples? The Lippman book (p. 197) prefers to define the > function type to return a reference to the class while the Pohl book > (p. 125) seems to prefer to return the class. Both seem to work > equally well as both lvalue and rvalue. > > class counter { > int count; > public: > counter() { count = 0; } > counter(int c) { count = c; } > counter& operator ++() { count++; return *this; } > value() { return count; } > }; > > class counter { > int count; > public: > counter() { count = 0; } > counter(int c) { count = c; } > counter operator ++() { count++; return *this; } > value() { return count; } > }; > One difference I can think of is that the first class allows the cascade of "++" in this case, that is: counter i; (i++)++; cout << i.value() << '\n'; // gives 2 while the second one gives 1. Alex Wu Sun Microsystems, Inc. 415-336-6874 acw@sun.com