Alex Rivera | Logout

Reducing code duplication between operator= and the copy constructor

Asked 2009-09-25T12:57:32.400
23

I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator?

Edit
Report

1 Answer

8
My &My::operator = (My temp)  // thanks, sellibitze
{
    swap (*this, temp);
    return *this;
}

and implement a specialised std::swap<> (My &, My &).

answered 2009-09-25T13:05:04.553

Your Answer