I'm attempting to call my copy constructor in my assignment operator overload. I assign the result of the copy constructor to a pointer called temp, then set the this pointer to equal temp. This results gives me the error: lvalue required as left operand of assignment. I thought this was a pointer to my object. Why is there an issue with reassigning the pointer to a different object?
In the passing case I defereence this(*this) and set it equal to temp. To me this means I'm saying the actual object is equal to a pointer. Am I misinterpreting the meaning of this?
Passing case:
Foo *temp = new Foo(other); //invokes copy constructor
*this = temp;
Failing case:
Foo *temp = new Foo(other); //invokes copy constructor
this = temp;
Full source code:
TreeNode::TreeNode(TreeNode *other) {
this->leftChild = other->leftChild;
this->rightChild = other->rightChild;
this->data = other->data;
}
TreeNode& TreeNode::operator=(TreeNode *other) {
if(this != other) {
TreeNode *temp = new TreeNode(other);
this = temp;
}
return *this;
}