Answer Posted / phool chand
A Copy Constructor constructs a new object as a copy of an existing object of the same type. Frequently copy constructors do a 'deep copy' of the object. X( const X& X_object ){...}; is a copy constructor for class X.
Deep Copy vs. Shallow Copy:
a shallow copy simply copies the contents of an object directly - if the object contains pointers, both the old copy and the new copy contain pointers to the same actual item. In a deep copy, when an object contains a pointer, a new copy of whatever the pointer points AT is created and the new object contains a pointer to the newly created copy of the item.
Why are deep copies important? If you carry out a shallow copy you end up with two pointers to the same item. If that item is an object with a destructor, this generally means you'll end up calling the destructor for that item twice, which will generally cause problems.
Unfortunately, most don't know to ask this question directly: the symptom is generally heap corruption which is hard to track down directly since there it has many possible causes.
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Why c++ is the best language?
Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].
Do you know the use of vtable?
What is polymorphism & list its types in c++?
How many keywords are used in c++?
Explain friend class?
Do vectors start at 0?
Why do you use the namespace feature?
Explain the difference between using macro and inline functions?
Explain about vectors in c ++?
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
Is string data type in c++?
Explain deep copy?
What is a wchar_t in c++?
Can you overload the operator+ for short integers?