Every class object can contain properties, methods or other class instantiations as part of it. There is always a need to copy content from one group to another. This can be done in two ways: using shallow copy or deep copy. But what is the difference between these two methods? Good way to show this is through examples.
For this example, let’s say that we have two classes Object_A and Object_B.
Class Object_A contains some fields: bit [7:0] addr, bit [7:0] data; Class Object_B contains field bit [4:0] delay.

Also, class Object_A has an object of class Object_B, obj_b0. Therefore, class Object_A contains a nested class called Object_B.
Inside module top, we are creating two objects of class Object_A, obj_a0 and obj_a1.

When we run the code above, log prints show that only variables and instanced handles are copied correctly, but nested objects are not entirely copied. Their handles will be assigned to a new object, but both obj_a0 and obj_a1 will point to the same nested object instance obj_b0. Other variable values are changed correctly, but the value of variable delay is the same in both copies. This type of copy is called shallow copy.
On Figure 1, a memory allocation of each variable is shown. When executing a shallow copy, variables such as obj_a1.data and obj_a0.data, are written on different memory locations, but internal objects (like nested class) share the same memory location.

This is why we see the same value for delay in both objects obj_a1 and obj_a0.
To overcome this situation, a deep copy method is used.
A deep copy method is where everything inside a class (also nested objects) is copied.
Let’s change the code above. We will make a copy function inside class Object_A.
Other contents of previous classes are left the same.

Now, we can see that we have copied obj_a0_d to obj_a1_d, but here the difference is, that obj_a0_d.obj_b0.delay variable from the nested class is not the same, but has a different value then obj_a1_d.obj_b0.delay. This is because we have used a custom made function copy instead of function new() for copying class object obj_a0_d to obj_a1_d.
Whenever a function copy is called, it will create a new object and copy all the class properties to a new object handle and return the new object handle.
The log results are proving how deep copy can be executed.

On Figure 2, a memory allocation of each variable is shown. When executing a deep copy code, variables such as obj_a1_d.data and obj_a0_d.data, are written on different memory locations. Also, internal objects (like nested classes) are written to a different memory location, they do not point on the same location in memory like they do when shallow copy is executed.
