What if I don't use copy constructor? Where does it create problem?
There is no problem as such if we don't use copy constructors. It is just a mechanism to create a new object before initializing with another object.
If we don't have copy constructor then Base b; Base c = b; this second line will not create a new object, rather it will do bit wise copy of object b to c. And there by both the objects c,b having same memory copy. This can create problems in some situations. To avoid this we use copy constructor.
both copy constructor and assignment operator are same. So it really doesn't matter if you have copy constructor or not. You can simulate copy constructor using assignment operator also.
all are wrong.
Assume that Base is the class name
and Base class has only a normal constructor with out any parameters, and it doesn't have copy constructor.
Base b; //here it will call normal constructor
Base c = b;
Now here in the second line it will not call normal constructor, rather it will do bit wise copy of object b to object c, and potentially creating two objects having same memory copy.
This can create problems, like if you delete both b,and c then both will clean same memory copy. To avoid these kind of problems of having same memory copy, we use copy constructor to create a new object with new memory, and then copy only the contents of the object to newly created object.
Back To Top