I came across this code example in chapter 18 of Stroustrup's "Programming Principles and Practices with c++ 2nd ed." Book.
vector& vector::operator=(const vector& a)
// make this vector a copy of a
{
double* p = new double[a.sz]; // allocate new space
copy(a.elem,a.elem+a.sz,elem); // copy elements
delete[] elem; // deallocate old space
elem = p;
sz = a.sz;
return *this;
}
The above example seems suspect to me. Based on my understanding, I would expect the copy function to copy into p instead of elem. Is the code right or is my fundamental understanding of this concept faulty?