From various posts including this one I have noticed the copy and swap idiom however I am not sure how to implement this code when it comes to derived classes. The code there is specified as
dumb_array& operator=(dumb_array other) // (1)
{
swap(*this, other); // (2)
return *this;
}
I understand that whenever a custom assignment operator is defined in a derived class then it is the responsibility of that methods to call the assignment operator of the base class. How would I go about optimally calling the assignment operator of the base class in this case. This is my attempt
dumb_array& operator=(dumb_array other)
{
swap(*this, other);
baseclass::operator= other; //Suppose base class has assignment operator resembling this
return *this;
}
My understanding is that if we call the base class assignment operator from the derived class then the copy constructor (because the parameter is value type) will be called twice (one for derived and then again for base)( and this is only if if it was a single chain of inheritance) what would be the optimal way to call the copy assignment operator of the derived class along with the base class ? Will we need to make changes to the swap method ?