What is deep and shalow copy?

Answer Posted / hemanth

Deep Copy and Shallow Copy

The terms "deep copy" and "shallow copy" refer to the way
objects are copied, for example, during the invocation of a
copy constructor or assignment operator. In a deep copy
(also called "memberwise copy"), the copy operation
respects object semantics. For example, copying an object
that has a member of type std::string ensures that the
corresponding std::string in the target object is copy-
constructed by the copy constructor of class std::string.


class A
{
string s;
};
A a;
A b;
a=b; //deep copy
When assigning b to a, the compiler-generated assignment
operator of class A first invokes the assignment operator
of class std::string. Thus, a.s and b.s are well-defined,
and they are probably not binary-identical. On the other
hand, a shallow copy (also called "bitwise copy") simply
copies chunks of memory from one location to another. A
memcpy() operation is an example of a shallow copy. Because
memcpy() does not respect object semantics, it will not
invoke the copy constructor of an object. Therefore, you
should never use memcpy() to copy objects. Use it only when
copying POD (Plain Old Data) types: ints, floating point
numbers, and dumb structs.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is inheritance and how many types of inheritance?

624


i got a backdoor offer in process global,Bangalore..Can i work with it?

2330


What is difference between class and object with example?

566


What is data binding in oops?

590


Why do we need polymorphism in c#?

695






Plese get me a perfect C++ program for railway/airway reservation with all details.

3432


How to hide the base class functionality in Inheritance?

642


write a code for this. serial_number contained in the header of the file will be read , if this serial number is less than a previous serial number within a successfully processed file, or is the same as another serial number within a successfully processed file, or if the field contains anything other than 7 digits, then the file must error with the reason ‘Invalid SERIAL_NUMBER’.

1784


What is polymorphism what are the different types of polymorphism?

568


which feature are not hold visual basic of oop?

1727


What is abstract class in oops?

604


What do you mean by abstraction?

622


Please send ford technologies placement paper 2 my mail id

1660


What is the real time example of encapsulation?

602


What are the three parts of a simple empty class?

1462