One way to implement the assignment operation is using the following steps:
Note that the logic in steps 1.b, 1.c, 1.d, and 1.e is identical
to that of the copy constructor.
The other steps (1, 1.a, and 2) reflect the differences between
the assignment operation and the copy constructor operation.
- If original and I are two distinct lists
(Note: use & and this, as we've done previously):
- Deallocate my chain of nodes.
- Set myFirst and myLast to NULL.
- Set mySize to zero.
- Declare nPtr containing the address of
original's first node.
- While nPtr is pointing at something:
- Append (to me) a copy of the item in the node to which
nPtr is pointing.
- Advance nPtr to the next node.
- Return myself.
Rather than copy-and-paste the copy constructor's logic
into your assignment operator
(propagating any logic errors it might contain),
consider highlighting the body of the copy constructor
and then using Eclipse's
Refactor > Extract function
mechanism to extract the body of the copy constructor
into a method named makeCopyOf().
Eclipse will replace the body of the copy constructor
with a call to that method,
and you can invoke that method in your assignment operator
to perform steps 1.b, 1.c, 1.d, and 1.e.