Student billy {...};
Student bobby {billy}; /// copy ctor
Student jane;
jane = billy; // copy assignment
jane.operator = (billy);
n1=n2; // n1.operator=(n2);
n1=n2=n3; // n2.operator = (n3);
EXAMPLE:
Node & Node::operator=(const Node &other) {
if (this==&other) return *this; // self assignment check
data = other.data;
delete next; // since we are updating on existing obj
next = other.next? new Node {*other.next}=nullptr;
return *this;
}
If new fails, stop executing
next is a "dangling pointer"
New assignment operator:
Node &Node::operator=(const Node &other) {
if (this==&other) return *this;
Node *temp=next;
next = other.next? new Node {*other.next}=nullptr;
data = other.data;
delete temp;
return *this;
}
// Switch of order; keeping temp in class
// Assign new, then delete