我在 Stroustrup 的“C++ 编程原则和实践第 2 版”的第 18 章中遇到了这个代码示例。预订。
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;
}
上面的例子对我来说似乎很可疑。根据我的理解,我希望复制函数复制到 p 而不是 elem。代码正确还是我对这个概念的基本理解有误?