One way to implement the assignment operation is using the following steps:
Note that steps 1.b, 1.c, and 1.d use logic identical
to that of 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: this is different from inequality; think this and &):
- Deallocate my chain of nodes.
- Set myFirst and myLast to NULL.
- Set mySize to zero.
- If original is not empty:
- 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, and 1.d.