Stroustrup 的“编程原则与实践”一书中的这个代码示例是否有错误?

时间:2021-08-01 00:01:22

标签: c++ pointers vector

我在 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。代码正确还是我对这个概念的基本理解有误?

0 个答案:

没有答案