i'm currently learning move and copy assignments, and i would value your input and suggestions on taking what i currently have coded into a different direction.
my main question: how do you reset the vector size?
vector &vector::operator=(const vector &rhs) // copy assignment
// make this vector a copy of the rhs (i.e. source)
{
double *pD = newdouble[rhs.vsize]; // allocate new space for double[]
std::copy(rhs.elem, rhs.elem + rhs.vsize, pD); // use std::copy algorithm to copy rhs elements into pD double[]
delete[] elem; // deallocate old space
elem = pD; // now that we've copied new, deallocated old elems, reset elem pointer
// reset vector size
return *this; // return a self-reference
}
vector &vector::operator=(vector &&rhs) // move assignment
// move rhs (i.e. source) to this vector
{
delete[] elem; // deallocate old space
elem = std::move(rhs.elem); // copy rhs’s elements and size, move implies copying element pointer only
rhs.elem = 0; // empty the rhs vector
rhs.elem = nullptr;
return *this; // return a self-reference
}
when i run the program, the copy constructor is invoked instead of move. How does this result in improved performance? (unless i specifically called that constructor despite having overloaded constructors?)