Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 10
    Use the copy-and-swap idiom. Commented Sep 6, 2010 at 15:28
  • Typically, the copy assignment operator will do some cleanup. If your class has a pointer to dynamically allocated memory, the first thing the copy-assignment operator should do is free that memory. This implementation of the copy constructor would give the copy assignment operator a dangling pointer, which you don't want to delete. Commented Nov 8, 2014 at 22:40
  • Even if you use smart pointers (in which case delete wouldn't an issue), you'd still be pointlessly default constructing and assigning all the member variables. Just use copy and swap. Commented Nov 8, 2014 at 22:43