Code:
Linked_list& Linked_list::operator= (const Linked_list& list) {
Linked_list::~Linked_list();
Node* node = new Node, *temp = list.head->next;
while (temp != nullptr) {
node = temp;
temp = temp->next;
}
head = list.head;
}
I'm making a class for linked list. I want the '=' to work so that all the nodes in the current object gets deleted and the nodes are inherited from the passed object.
Can I call Linked_list::~Linked_list(); like that? Here's what I have under the destructor:
Code:
Linked_list::~Linked_list() {
Node* node = head;
Node* previous_node;
while (node->next != nullptr) {
previous_node = node;
node = node->next;
delete previous_node;
}
}
The class has an integer data member. So would calling the destructor like that also destruct the int?
I also saw that you can write 'delete this;', does that achieve the same thing as here?