When should we write our own copy constructor?


Don’t write a copy constructor if shallow copies are ok: In C++, If an object has no pointers or any run time allocation of resource like file handle, a network connection..etc, a shallow copy is probably sufficient. Therefore the default copy constructor, default assignment operator, and default destructor are ok and you don’t need to write your own.

Source: http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:

2 Average Difficulty : 2/5.0
Based on 26 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.