In: Computer Science
In programming, we are always assigning one variable to another. With the use of primitive data types that is not a problem. With classes, the program needs to know how to do the assignment. Explain the concepts of deep copy and member wise assignment.
No duplicate answers please. C++ language
Memberwise/Shallow Copy
The variables A and B refer to different areas of memory, when B is assigned to A the two variables refer to the same area of memory. Later modifications to the contents of either are instantly reflected in the contents of other, as they share contents. Example below:
Deep copy:
The variables A and B refer to different areas of memory, when B is assigned to A the values in the memory area which A points to are copied into the memory area to which B points. Later modifications to the contents of either remain unique to A or B; the contents are not shared.
In short, it depends on what points to what. In a shallow copy, object B points to object A's location in memory. In deep copy, all things in object A's memory location get copied to object B's memory location.