Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Differentiate between a deep copy and a shallow copy?



Differentiate between a deep copy and a shallow copy?..

Answer / ritesh pal

Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, the two objects may contain ht same information but the target object will have its own buffers and resources. the destruction of either object will not affect the remaining object. The overloaded assignment operator would create a deep copy of objects.

Shallow copy involves copying the contents of one object into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable.

Explanation:

Using a copy constructor we simply copy the data values member by member. This method of copying is called shallow copy. If the object is a simple class, comprised of built in types and no pointers this would be acceptable. This function would use the values and the objects and its behavior would not be altered with a shallow copy, only the addresses of pointers that are members are copied and not the value the address is pointing to. The data values of the object would then be inadvertently altered by the function. When the function goes out of scope, the copy of the object with all its data is popped off the stack.

If the object has any pointers a deep copy needs to be executed. With the deep copy of an object, memory is allocated for the object in free store and the elements pointed to are copied. A deep copy is used for objects that are returned from a function.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

When is the destructor called?

0 Answers  


what are the characteristics of Class Members in C++?

0 Answers  


Explain how would you handle a situation where you cannot call the destructor of a local explicitly?

0 Answers  


Explain the difference between c++ and java.

0 Answers  


Can static member variables be private?

0 Answers  


Write a corrected statement in c++ so that the statement will work properly. if (x = y) x = 2*z;

2 Answers  


What happens if an exception is throws from an, object's constructor and object's destructor?

4 Answers   Wipro,


what is the difference between linear list linked representaion and linked representation? what is the purpose of representing the linear list in linked represention ? is it not avoiding rules of linear represention?

0 Answers  


Can we declare a base-class destructor as virtual?

0 Answers  


Explain mutable storage class specifier.

0 Answers  


show that among any group of five (not necessary consecutive ) integers, there are two with the same remainder when divided by 4.

1 Answers  


What is DlgProc?

0 Answers   C DAC,


Categories