Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in C++, How can I give a class copy constructor and assignment operator the same functionality without making duplicate code [duplicate]

Possible Duplicates:
What is the copy-and-swap idiom?
Copy constructor and = operator overload in C++: is a common function possible?

Is there a way that I can make the body of the copy constructor and assignment operator contain the same code without actually having duplicate code (except for the function headers)?

like image 232
Matt Munson Avatar asked Feb 22 '11 00:02

Matt Munson


1 Answers

One common way is the copy-and-swap idiom. You would have to implement a swap operation, but if done correctly, you have the additional benefit of having exception safe assignment.

like image 107
Pablo Avatar answered Oct 29 '22 22:10

Pablo