Murali said:
yes I agree Rolf. I have a query.
In an interview, they asked my friend about shallow and deep copy.
he could not explain because we never heard about that.
After that some one said..
deep copy happens through default constructor
and shallow copy is thru copy constructor. I think I did not match them
correctly.
plz correct which is deep and shallow.
You are right that this definition is not correct.
A deep copy is a copy where the whole content of the object is copied, while
a shallow copy doesn't copy the data itself, but e.g. just its address, so
that the original object and the copy share their data.
The compiler-generated copy constructor does a deep copy. If you write your
own, you can do anything you want. The default constructor has nothing to
do with it.
and also said..
Copy constructor is dangerous. While programming with pointers, it may
lead to exception.
Well, if you have several objects that have a pointer to the same data, you
have to ensure that it's not deleted multiple times or used after being
deleted. However, failure to handle it correctly most often leads to
undefined behavior. An exception might be thrown, but typically isn't.
and shallow (copy constructor) will not have that problem.
Actually, it's just the shallow copy that has that problem, because it
usually means that after the copy operation, two objects have a pointer to
the same data. A deep copy is unproblematic in that respect, but might be
costly for large amounts of data.