Copy Constructors: Are They Necessary

I understand the concept of a deep copy and why passing objects by name alone doesn't work. The thing is that, I was always taught to include certain things in every data structure in C++. Such things included a copy constructor and an overloaded assignment operator.

My question is, why do you need both? As far as I can tell they are two ways of doing the same thing.
The copy CONSTRUCTOR is only accessible when CONSTRUCTING an object.
The ASSIGNMENT operator is only available when ASSIGNING (and the object is already constructed).
Please note that since c++11 it is also recommended to provide move constructor and move assignment.
Only if the defaults provided by the compiler are not good enough.


Follow the so called rule of three: If you need a custom destructor, copy constructor or assignment operator, then you need the three.

By instance, you may need them if you acquire a resource (like memory) and you need to release it.
Topic archived. No new replies allowed.