Deep copy method

What is the difference bw the following deep copy methods?


Class A;
int a;
.....
function A copy();
copy=new();
copy.a=this.a;
endfunction
.....
endclass

In the above code the return type is class and the object and function name are same. How can we write like this?


Class A;
int a;
....
function void copy(A ab);
this.a=new ab;
endfunction
....
endclass

In this case we are declaring a copy function to that we are passing the object. Can anyone tell me how the above codes will work?

Thank You,

In reply to Manoj J:

The first method is really a “construct a copy”, normally called the “clone” method.

The second method is a true copy from the object being passed as an argument to copy() to the implicit ‘this’ argument that copy was called on.

class A;
  int al;
  function A clone();
    clone = new();
    clone.copy(this);
  endfunction
  function void copy(A ab);
    this.a=ab.a;
  endfunction
endclass

And deep copy refers to copying classes that contains other class variables.

In reply to dave_59:

Thank you dave!!