1_ You are missing the point.
You need to copy all the list, you know that you finish when you reach NULL.
¿So why
theirs->next != NULL
instead of
theirs != NULL
?
Because I've started at the head. (¿is that a good reason?)
You can easily traverse the other list. The problem here is to link the cells.
Suppose a list of 3 elements.
_ Fist you copy the heads.
this->head = new stackNode( other.head->value );
_# Get to the next element
stackNode *theirs = other.head->next;
_ Copy that
stackNode *aux = new stackNode( theirs->value );
_ And link (¿can you do it?)
_ If you didn't finish get back to #
2)
Bjarne Stroustrup wrote: |
---|
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient. |
You need to delete it when you end using it (the object).
The memory leak occurs when you lose the memory address of an allocated object.
By instance
1 2 3
|
ptr = new derived;
ptr = new derived; //we lose the previous object, leak
ptr = NULL; //and again
|
A NULL pointer can't be pointing to allocated memory, so lines 86-87 are superfluous (conceptual error).