Regarding your copy constructor, you have an insert_at_tail, so just iterate the argument and insert the elements in the list:
1 2 3 4
|
StudentRoll::StudentRoll(const StudentRoll &orig) {
for(Node* n = orig.head; n; n = n->next)
insertAtTail(*n);
}
|
Copy assignment operators usually follow one of a few patterns, and so are a little easier to write, if more complicated.
See this, and the posts linked from it:
http://www.cplusplus.com/forum/beginner/215692/#msg1001849
There are two steps to an assignment operator:
1.) Release the old resources; then
2.) Copy the new ones from the right-hand-side.
You release old resources in the destructor, and copy resources in the copy constructor. Don't duplicate code -- you can call them:
1 2 3 4 5 6
|
StudentRoll& operator=(StudentRoll const& rhs) {
if (std::addressof(rhs) == this) return *this; // careful for self-assignment
~StudentRoll(); // release old resources
new(this) StudentRoll(rhs); // storage reuse -- copy from rhs
return *this;
}
|
Preferably, copy-and-swap provides a strong exception guarantee by doing things in this order:
1.) Make a temporary copy of the new resources (which might throw)
2.) Switch your old resources with the temporary copy (which must not throw)
3.) Destroy the temporary copy of your old resources.
1 2 3 4
|
StudentRoll& operator=(StudentRoll rhs) {
swap(*this, rhs);
return *this;
}
|