'Is there a mistake in this code example in Stroustrup's "Programming Principles and Practices" book?

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?



Solution 1:[1]

You are correct, and so is JeremyFriesner's comment, which points out that it should be:

std::copy(a.elem, a.elem.sz, p);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cigien