wesome, thanks. why the & in the function definition? does that just return a reference?
Thanks, Tyler Littlefield http://tysdomain.com ----- Original Message ----- From: Thomas Hruska To: [email protected] Sent: Wednesday, January 14, 2009 9:12 PM Subject: Re: [c-prog] operator overloading:what to return? Tyler Littlefield wrote: > Hello list, > I've got a quick question. > I'm writing a linked list library for a program I'm working on. > I was trying to make this versatile, if possible, and wanted to add in some operator overloading. > This looks vairly easy, but I'm not totally sure what to return. > Basically my block of code looks like this: > { > this->Append(val); > } > the prototype is: > LList* operator +=(T val) > I'm not sure if I should return this, and then make another that is just LList operator +=... or just return void, I'd like to support something like: > i+=x; > well, there isn't much of a way not to support it. > I could return void, and I would remain unchanged I think. > Ideas would be welcome. this-> is implied. You only need to call Append(val); I'm not sure what your template is defined as, but your code should look something like: LList<T> &operator+=(const T &val) { Append(val); return *this; } Fairly standard practice to return the dereferenced this pointer. This allows you to do weird stuff later on: LList<int> MyList; ((MyList += 5) += 10) += 15; Don't know what would possess anyone to do that, but you could. It is more useful/meaningful with other operators. -- Thomas Hruska CubicleSoft President Ph: 517-803-4197 *NEW* MyTaskFocus 1.1 Get on task. Stay on task. http://www.CubicleSoft.com/MyTaskFocus/ __________ NOD32 3767 (20090115) Information __________ This message was checked by NOD32 antivirus system. http://www.eset.com [Non-text portions of this message have been removed]
