//* Sample dynamic array - assignment operator variations *

/* this_variable = rightHandSide; OR this_variable.operator=(rightHandSide); FRIEND version is doable but less desirable most of this applies also to the copy constructor variations */

//* efficient in saving memory *

const ADList& ADList::operator=(const ADList & rightHandSide) { if (this!=&rightHandSide) { delete [] myArray; myCapa=rightHandSide.mySize; mySize=rightHandSide.mySize; myArray=new ElType[myCapa]; for(size_t i=0; i<mySize; i++) myArray[i]=rightHandSide.myArray[i]; } return(*this); }

//* preserve the capacity through the assignment *

const ADList& ADList::operator=(const ADList & rightHandSide) { if (this!=&rightHandSide) { delete [] myArray; myCapa=rightHandSide.myCapa; mySize=rightHandSide.mySize; myArray=new ElType[myCapa]; for(size_t i=0; i<mySize; i++) myArray[i]=rightHandSide.myArray[i]; } return(*this); }

//* duplicate including garbage, inefficient but making the copy truly identical *

const ADList& ADList::operator=(const ADList & rightHandSide) { if (this!=&rightHandSide) { delete [] myArray; myCapa=rightHandSide.myCapa; mySize=rightHandSide.mySize; myArray=new ElType[myCapa]; for(size_t i=0; i<myCapa; i++) myArray[i]=rightHandSide.myArray[i]; } return(*this); }

//* optimized both for speed and size - does not resize if enough capacity *

const ADList& ADList::operator=(const ADList & rightHandSide) { if (this!=&rightHandSide) { if (myCapa<rightHandSide.mySize) { delete [] myArray; myCapa=rightHandSide.mySize; myArray=new ElType[myCapa]; } mySize=rightHandSide.mySize; for(size_t i=0; i<mySize; i++) myArray[i]=rightHandSide.myArray[i]; } return(*this); }

//* variation of efficient in saving memory - not cascadable *

void ADList::operator=(const ADList & rightHandSide) { if (this!=&rightHandSide) { delete [] myArray; myCapa=rightHandSide.mySize; mySize=rightHandSide.mySize; myArray=new ElType[myCapa]; for(size_t i=0; i<mySize; i++) myArray[i]=rightHandSide.myArray[i]; } } // end of file