I'm implementing a linked list. I wrote a copy constructor:
// --- copy constructor ---
IntList(const IntList& last_list) {
first = new IntNode(last_list.getFirst()->getData());
cout << "copy first " << first->getData() << endl;
IntNode* last_node = first;
IntNode* node = last_list.getFirst()->getNext();
while(node!=NULL) {
IntNode* new_node = new IntNode(node->getData());
last_node->setNext(new_node);
cout << "copy " << new_node->getData()<< endl;
last_node = new_node;
node = node->getNext();
}
}
As I understand, my copy-assignment operator (operator=
) should have 2 goals:
- delete the current list.
- copy the new list.
I can achieve this 2 goals by calling the destructor which I already wrote and then call the copy constructor. How can I do it?