tecknophreak
04-16-2002, 10:14 PM
i can't remember which should be written first. for some reason i seem to remember operator coming first.
|
Click to See Complete Forum and Search --> : operator+= or operator+ first? tecknophreak 04-16-2002, 10:14 PM i can't remember which should be written first. for some reason i seem to remember operator coming first. debiandude 04-16-2002, 11:19 PM In C its +=, which makes sense becuase =, +=, and -= are evaluted from right to left. i =-b would set i equal to the negative of b, since the minus is on the right modifies b before setting i equal to it. [ 17 April 2002: Message edited by: debiandude ] kmj 04-18-2002, 09:05 AM Originally posted by tecknophreak: <STRONG>i can't remember which should be written first. for some reason i seem to remember operator coming first.</STRONG> What do you mean by the phrase "written first"? If you mean time-wise then probably the operator+, so that you can use it when you write operator+=; of course, you probably also want to have operator= written too. Of course, I'm just guessing at what you're asking, and I'm assuming that debiandude guessed wrong at what you were asking. So, please be a little more clear in what your asking... (c: bwkaz 04-18-2002, 09:07 AM Err? I think he means "which one should I write first, and which one should use the other?" Like if he wrote operator += first, then operator + would create a temporary object (initialized to whatever got passed in), then he'd use += on it to add the other argument, then he'd return a copy of the temporary. If he wrote operator + first, then he could use it in operator +=, with the same end result. And as a matter of fact, I have no idea which is "supposed" to be done first either. tecknophreak 04-18-2002, 09:47 AM bwkaz was right. when i was taking some c++ courses, i was told that if you wrote one first(like +=) then use it when writing +, it'll be faster than if you write + first and use it in writing +=. however i have forgotten. Sorry bout the confusion. debiandude 04-18-2002, 06:00 PM Woah I guess I was totllly off base with that one. I should have know becuase I seen some code that you have posted before techno so I don't know I didn't just realize that you obviously wouldn't ask such a rudimentary question. So this was about some wierd C++ mumbo jumbo ;-) I prolly should look in to C++ maybe someday. tecknophreak 04-19-2002, 07:55 AM my bad. i'll have to start adding some more info on my posts. :( C++ mumbojumbo.. c++ is good. me like tecknophreak 04-25-2002, 12:54 PM just for an update, you should create operator+= first then use it when creating operator+. when you use operator+, you need to create a new container. when you use operator+= you don't. so if you use operator+ in operator+=, you're creating a new container which you don't need to create. hope this makes some sense. Stuka 04-25-2002, 01:44 PM OK, the not making a container makes sense, but I'm having difficulty with using += in +.... tecknophreak 04-25-2002, 04:49 PM i'll have to get back to you on that one...i forgot how to make it so. :rolleyes: bwkaz 04-25-2002, 06:33 PM Just for example, assume MyClass is a wrapper for ints or something. The only private member is "value": void operator +=(MyClass &a, const MyClass &b) { a.value += b.value; } MyClass operator +(const MyClass &a, const MyClass &b) { MyClass c = a; // Assumes that MyClass::operator =(const MyClass &) has been properly defined and implemented c += b; return c; } Make sense? [ 25 April 2002: Message edited by: bwkaz ] Stuka 04-26-2002, 02:29 PM Ahh...makes perfect sense... justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |