overloading assignment operator

template<class TYPE_NAME>
Array<TYPE_NAME> &Array<TYPE_NAME>::operator=( Array &rhs )
{
if (this == &rhs)
return *this;

delete this;
this->rhs;
return *this;
}

I'm trying to overload the = operator by deleting allocated memory, assigning the new value to the new memory and returning the new value. I'm getting a compiler error saying you can't assign a value to the this pointer. Is there an easy fix for this?

you should not delete the this pointer, instead you should delete all the pointer variables in the class and recreate and reassign them with the new one..

Edit: deleting this pointer and then accessing it will cause your application to crash.
Last edited on
Got it working. Thanks.
Topic archived. No new replies allowed.