Actually, I’ve been cheating slightly - if Whole is to obey the “rule of least surprise” it will not only have an assignment operator but also a copy constructor and (given the earlier discussion) a swap() operation that doesn’t throw. Using these the assignment operator can be written in two lines [above].
This doesn’t mean that the previous example was wasted - the techniques are applicable in many circumstances where several parts of an object need to be updated. However assignment is a special case which requires updating the whole object.
I’m going to come back to the subject of writing the copy constructor and swap operations, but first I want to replace std::auto_ptr<> with something more suitable to the needs of the current class. We will also look at the impact of there being a base class.