Hi,

I'm not sure if the book is incorrect or I simply cannot understand this code yet:

Code:
class vector {
	int sz;
	double* elem; // pointer to the elements
	void copy(const vector& arg);
public:
	vector& operator=(const vector&);
};

void vector::copy(const vector& arg)
{
	for (int i = 0; i<arg.sz; ++i) elem[i] = arg.elem[i];
}

vector& vector::operator =(const vector& a)
{
	double* p = new double[a.sz];  // allocate new space
	copy(a);  // copy elements
	delete[] elem;  //deallocate old space
	elem = p;  // reset elem
	sz=a.sz;
	return *this;
}
The part I don't understand is the operator overload function. The new space has been allocated and then elements copied, but where are these elements copied to? According to the copy function, they are copied to elem[i] of the vector being copied to. However, in the = overload, that elem is deleted immediately after the copy function is called and then reset with p. Are the elements copied into p, if so how?

Thanks.