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/