Shallow and deep copy

HI All,

consider the class/general example like this:

class A;
    integer i = 1;
  endclass

  class B;
    integer j = 1;
    A obj_a = new;
  endclass

B b1, b2;
  1. object assignment
    b1 = new;
    b2 = b1;

  2. shallow copy
    b1= new;
    b2 = new b1;

  3. deep copy
    b1= new;
    b2 = new;
    b2.copy(b1)

Query: Where all these 3 will be used?
I searched in few websites, could not find the application of all these.
I wanted to understand more, where these kind of copy statments are used?
can you please give one or two examples?

Thank You,

Object assignment gets used when calling a method. It’s how assignments are made to method arguments.

Shallow copy and deep copy are concepts that in practice get blended together. I suggest you take a look at my SystemVerilog OOP course on classes.

Hi @dave_59
Suppose we created a class and define some property and methods in class, and create copy of that class using shallow copy. Will the methods will also gets copied or only properties of that class will be copied.

Properties and methods are defined as part of a class type. When constructing/creating a class object, space gets allocated to hold values associated with the class properties. There is no space allocated for the variables of a method until someone calls the method. The space gets released when exiting the method. That is how automatic lifetime works.