The correct way to implement the assignment operator is to use
Student& operator=(const Student& s){
if (&s != this){
this->name = s.name;
/*etc for all members*/
}
return *this;
}
A few things to note:
The
s
to copy is passed by constant reference. That means that it does not take a deep copy nor is allowed to be modified by the function.Return
*this
allows you to do multiple assignments:a = b = c;
The
if
statement circumvents any problems arising from self-assignment.Pay special attention to the copying of
classList
. Ensure a deep copy is taken.
But It's far better to use the standard template library containers so you can rely on the compiler-generated assignment operator.