My teacher deducted points for my copy constructor. This was what I did and what he wanted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// THIS IS WHAT I DID
template <class T>
Set<T>::Set(const Set<T> & rhs){
*this = rhs;
}
// THIS IS WHAT HE WANTED
template <class T>
Set<T>::Set(const Set<T> & rhs){
SetSize = 0;
SetCapacity = 0;
ElementArray = NULL;
*this = rhs;
}
An assignment operator expects to operate on an object with valid values. If you're in a constructor, you must given your data members valid values before calling the assignment operator. For instance, in the assignment operator, delete is likely to be called on ElementArray, resulting in undefined behavior if ElementArray contains some random value.
It's possible that your = operator makes assumptions about the object state that you haven't initalized yet. For example, if 'ElementArray' points to dynamically allocated memory, your = operator probably assumes that pointer is valid, which -- in your copy ctor, it isn't (because you haven't initalized it yet).
Generally speaking, you want your constructors to put your object in a known state. Whereas all other members can assume the object has already been properly constructed.
Your = operator an assume the object has been constructed -- but this means that you must actually construct it before calling the = operator.