# Importance of the clone( ) method

**URL:** <https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472>\
**Category:** OVM\
**Tags:** OVM\
**Created:** [March 28, 2008, 11:34am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472 "2008-03-28T11:34:52Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit11](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@ankit11](https://verificationacademy.com/forums/u/ankit11)\
**Post date:** [March 28, 2008, 11:34am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/1 "2008-03-28T11:34:52Z")

</div>

Hi ALL,

Can anybody tell me the exact application of the clone methos?  
What is the significance of the clone method?

Regards,  
Ankit.

---

<div class="post-metadata">

**Author:** ![kurts1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@kurts1](https://verificationacademy.com/forums/u/kurts1)\
**Post date:** [March 28, 2008, 6:56pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/2 "2008-03-28T18:56:32Z")

</div>

The clone method is used to provide a deep (nested) copy of an object.

clone first allocates new memory for the object, then copies over each field to the new object. If a field is an object handle, then instead of copying the handle (which would do a “shallow” copy) you would call fieldname.clone() to recursively allocate memory for that field (a “deep” copy).

-Kurt

---

<div class="post-metadata">

**Author:** ![Gopi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@Gopi](https://verificationacademy.com/forums/u/Gopi)\
**Post date:** [March 31, 2008, 5:47am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/3 "2008-03-31T05:47:36Z")

</div>

clone() returns a handle of newly created object which is copy of original object.

If you want to use copy(), New objected has to be created explicitly.

Look at page number 267, 284 of AVM cook book.

---

<div class="post-metadata">

**Author:** ![devangamoneesh](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/5f9b8f/32.png) [@devangamoneesh](https://verificationacademy.com/forums/u/devangamoneesh)\
**Post date:** [March 31, 2008, 6:40am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/4 "2008-03-31T06:40:02Z")

</div>

Hi Ankit,

you can simply remember as clone = **create** and (+) copy  
That is, it first creates a new object and then copies all the data to the newly created object.

Regards,  
Moneesh

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 31, 2008, 8:10am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/5 "2008-03-31T08:10:51Z")

</div>

Another key difference is that \*\*clone()\*\*is _virtual_;\*\*copy()\*\*is _non-virtual_. And **clone()** is a method of the original object; **copy()** is a method of the target object.  
That means **clone()** creates a copy an object of the same class type as the original. With **copy()**, the original object could be a subclass of the target object’s type with additional class properties that will not get copied.

Dave

---

<div class="post-metadata">

**Author:** ![mseyunni](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@mseyunni](https://verificationacademy.com/forums/u/mseyunni)\
**Post date:** [February 24, 2014, 7:06pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/6 "2014-02-24T19:06:24Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/6):_

Hi Dave,

```
With copy(), the original object could be a subclass of the target object's type with additional class properties that will not get copied.

```

I haven’t understood why “additional class properties will not get copied” for copy() as the subclass (child) is inherited from a base class (parent), which means it has all the properties and methods of the parent. Could you explain what you meant by additional class properties not getting copied.

Thanks,  
Madhu

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 24, 2014, 9:16pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/7 "2014-02-24T21:16:43Z")

</div>

_In reply to [mseyunni](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/7):_

```verilog
class base;
int p1;
  function void copy(base orig);
    this.p1 = orig.p1;
  endfunction
endclass
class ex_base;
  int p2;
  function void copy(base orig);
    super.copy(b);
    this.p2 = orig.p2;
  endfunction
endclass

base b1,b2;
ex_base eb1, eb2;
initial begin
   eb1 = new; eb2 = new();
   eb2.p2 = 5;
   b1 = eb1; b2 = eb2;
   b1.copy(b2); // p2 is not copied
   eb1.copy(eb2); // p2 is copied
end

```

Since copy() is not virtual, calling b1.copy() calls base::copy(), and the additional property p2 is not copied even though it exists in object referenced by b1.

---

<div class="post-metadata">

**Author:** ![ranjithkumard](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@ranjithkumard](https://verificationacademy.com/forums/u/ranjithkumard)\
**Post date:** [January 6, 2015, 9:57am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/8 "2015-01-06T09:57:38Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/8):_

p2 will copy if we write b1.copy(eb2)?

---

<div class="post-metadata">

**Author:** ![Nico75](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/df788c/32.png) [@Nico75](https://verificationacademy.com/forums/u/Nico75)\
**Post date:** [October 22, 2015, 8:54am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/9 "2015-10-22T08:54:39Z")

</div>

I do not understand the claims about copy() here. I agree with the example code, but in UVM the non-virtual copy() method calls the virtual do\_copy() method. So the do\_copy of the derived class gets called, and hence its members do get copied?

For example:

```verilog

class base extends uvm_object;
    `uvm_object_utils(base)

    int f1=0;

    function void do_copy(uvm_object rhs);
        base rhs_;
        $display("do_copy of base");
        super.do_copy(rhs);
        $cast(rhs_,rhs);
        f1=rhs_.f1;
    endfunction
endclass

class ex extends base;
    `uvm_object_utils(ex)

    int f2=0;

    function void do_copy(uvm_object rhs);
        ex rhs_;
        $display("do_copy of ex");
        super.do_copy(rhs);
        $cast(rhs_,rhs);
        f2=rhs_.f2;
    endfunction
endclass

module automatic Top();

function void doit();
    base B1,B2;
    ex E1,E2;

    E1=new;
    E1.f1=123;
    E1.f2=456;
    B1=E1;
    E2=new;
    B2=E2;
    B2.copy(B1);
    $display("E2.f1=%0d",E2.f1);
    $display("E2.f2=%0d",E2.f2);

endfunction

```

calls the copy() on base class handles and still succesfully copies the correct derived class members:

```auto
# do_copy of ex
# do_copy of base
# E2.f1=123
# E2.f2=456

```

edit: I see this was actually an OVM topic, maybe this changed in UVM?

---

<div class="post-metadata">

**Author:** ![swethasundararaj](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@swethasundararaj](https://verificationacademy.com/forums/u/swethasundararaj)\
**Post date:** [January 6, 2016, 7:05am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/10 "2016-01-06T07:05:23Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/10):_  
Hi Dave,  
I can understand which you gave the example of copy but i need the example for clone() also.You said copy() is virtual in that thing i just confused what is virtual will you please clear my doudt i am new learner of SV.  
thanks in advance.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 11, 2016, 6:14pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/11 "2016-01-11T18:14:54Z")

</div>

_In reply to [NiLu](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/11):_

The OVM and UVM mechanisms are the same. The OVM added the virtual do\_copy() methods as a hook to the non-virtual copy() methods. This might be to get their field automation macros to work.

_In reply to [swethasundararaj](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/11):_

```verilog
class base;
int p1;
  function void copy(base orig);
    this.p1 = orig.p1;
  endfunction
  virtual function base clone(base orig);
    base h;
    h = new;
    h.copy(orig);
    return h;
  endfunction
endclass
class ex_base;
  int p2;
  function void copy(base orig);
    super.copy(b);
    this.p2 = orig.p2;
  endfunction
   virtual function base clone(base orig);
    ex_base h, h_orig
    h = new;
    $cast(h_orig, orig)
    h.copy(h_orig);
    return h;
  endfunction
endclass
 
base b1,b2;
ex_base eb1, eb2;
initial begin
   eb1 = new; eb2 = new();
   eb2.p2 = 5;
   b1 = eb1; b2 = eb2;
   b1.copy(b2); // p2 is not copied
   eb1.copy(eb2); // p2 is copied
end

```

---

<div class="post-metadata">

**Author:** ![scottj97](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@scottj97](https://verificationacademy.com/forums/u/scottj97)\
**Post date:** [August 1, 2016, 4:50pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/12 "2016-08-01T16:50:47Z")

</div>

_In reply to [NiLu](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/12):_

> I do not understand the claims about copy() here. I agree with the example code, but in UVM the non-virtual copy() method calls the virtual do\_copy() method. So the do\_copy of the derived class gets called, and hence its members do get copied?  
> For example:

```verilog

function void doit();
base B1,B2;
ex E1,E2;
E1=new;
E1.f1=123;
E1.f2=456;
B1=E1;
E2=new;
B2=E2;
B2.copy(B1);
$display("E2.f1=%0d",E2.f1);
$display("E2.f2=%0d",E2.f2);
endfunction

```

There is quite a bit of confusion in this thread on copy vs clone.

The problem with the quoted example is that the child class type must be known at compile time. Doing “E2=new; B2=E2;” means that B2 is pointing to type ex, so ex::do\_copy() will get run as intended.

But what if we don’t know the child class type? After all, that is the whole point. We have only a handle to an object whose specific subclass is not known at compile time. The copy() method does not work in this case. Here’s a few examples:

```verilog

///// option 1: no good because you must know type of rhs  
// E2=new;                                                    
// B2=E2;                                         
///// option 2: no good because B2 remains of type base
// B2=new;
// B2.copy(B1);
///// option 3: works
    void'($cast(B2, B1.clone()));
///// end options                                    
    $display("B2 type is %s", B2.get_type_name());

```

---

<div class="post-metadata">

**Author:** ![ankitjain](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@ankitjain](https://verificationacademy.com/forums/u/ankitjain)\
**Post date:** [March 7, 2019, 4:00am UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/13 "2019-03-07T04:00:28Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/13):_

> _In reply to [NiLu](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/13):_  
> The OVM and UVM mechanisms are the same. The OVM added the virtual do\_copy() methods as a hook to the non-virtual copy() methods. This might be to get their field automation macros to work.  
> _In reply to [swethasundararaj](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/13):_

```verilog
class base;
int p1;
function void copy(base orig);
this.p1 = orig.p1;
endfunction
virtual function base clone(base orig);
base h;
h = new;
h.copy(orig);
return h;
endfunction
endclass
**class ex_base;
int p2;
function void copy(base orig);
super.copy(b);
this.p2 = orig.p2;
endfunction
virtual function base clone(base orig);
ex_base h, h_orig
h = new;
$cast(h_orig, orig)
h.copy(h_orig);
return h;
endfunction
endclass**
base b1,b2;
ex_base eb1, eb2;
initial begin
eb1 = new; eb2 = new();
eb2.p2 = 5;
b1 = eb1; b2 = eb2;
b1.copy(b2); // p2 is not copied
eb1.copy(eb2); // p2 is copied
end

```

Hi dave,  
I am trying to copy an extended class object, randomized multiple times having single handle.  
It is to be copied to the same type of class object based on certain conditions, thus using cloning method.

I am trying to implement it like this:

```verilog
class mcs_data_packet extends mcs_inst_packet;

  rand logic w1r0;

  int mcs_core=0; 
  int count =0;   

  function new ();
  endfunction:new
  
  function void copy(mcs_data_packet ext);
    super.copy(ext);
    this.w1r0 = ext.w1r0; 
  endfunction
  
  virtual function mcs_data_packet clone(mcs_data_packet ext);
    mcs_data_packet h;
    h=new();
    h.copy(ext);
    return h;
  endfunction

```

But getting error as below:  
virtual function mcs\_data\_packet clone(mcs\_data\_packet ext);  
|  
ncvlog: \*E,CVMNMM (mcs\_data\_packet.svh,23|59): Virtual method ‘mcs\_data\_packet::clone’ formal argument name does not match base class ‘mcs\_inst\_packet’.  
virtual function mcs\_data\_packet clone(mcs\_data\_packet ext);  
|  
ncvlog: \*E,CVMTMM (mcs\_data\_packet.svh,23|59): Virtual method ‘mcs\_data\_packet::clone’ formal argument type does not match base class ‘mcs\_inst\_packet’.  
Total errors/warnings found outside modules and primitives:  
errors: 2, warnings: 0

But modifying the code:

```verilog
class mcs_data_packet extends mcs_inst_packet;

  rand logic w1r0;

  int mcs_core=0; 
  int count =0;   

  function new ();
  endfunction:new
  
  function void copy(mcs_data_packet ext);
    super.copy(ext);
    this.w1r0 = ext.w1r0;
  endfunction
  
  virtual function mcs_inst_packet clone(mcs_inst_packet ext);
    mcs_data_packet h,h_orig;
    h=new();
    $cast(h_orig,ext);
    h.copy(h_orig);
    return h;
  endfunction

```

Getting error as while cloning I am passing the extended class type to clone function while it is expecting base class type.

mcs\_data\_rec[0]=mcs\_data.clone(mcs\_data);   
|  
ncvlog: \*E,TYCMPAT (tb.sv,1131|30): assignment operator type check failed (expecting datatype compatible with ‘class $unit::mcs\_data\_packet’ but found ‘class $unit::mcs\_inst\_packet’ instead).

Can you please explain how to clone an extended class.

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [March 7, 2019, 1:50pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/14 "2019-03-07T13:50:46Z")

</div>

_In reply to [ankitjain](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/14):_

Both copy() and clone() are already defined as part of the uvm\_object class, and you don’t want to override them.

Instead, you want to implement do\_copy() instead. The clone() and copy() functions will utilize your do\_copy() method.

Refer to the [Transaction Methods](https://verificationacademy.com/cookbook/transaction/methods) page for more information.

---

<div class="post-metadata">

**Author:** ![svq](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@svq](https://verificationacademy.com/forums/u/svq)\
**Post date:** [December 30, 2019, 9:46pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/15 "2019-12-30T21:46:51Z")

</div>

I am still confused about the virtual/non virtual aspect of copy/clone after reading the thread. Can anyone please correct me if the following conclusion is wrong. do copy and do clone are virtual methods while copy and clone are not. However since there are hooks to do\_copy and do\_clone in copy and clone method. Hence calling copy and clone on the object will call the function implemented in the extended class object.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 30, 2019, 11:49pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/16 "2019-12-30T23:49:03Z")

</div>

_In reply to [svq](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/16):_

Unfortunately, the UVM is much more complicated than it needs to be due to its history. It is expecting that everyone is using the macros to register the class with the factory. You are not supposed to override clone() or copy(), so it really doesn’t matter if they are virtual or not in that case.

uvm\_object::clone() calls the virtual create() method that get inserted by the [`uvm\_object\_utils](https://verificationacademy.com/forums/uvm/uvm-utility-macros#reply-68981) macro in each derived class. There is no do\_clone() method; the create() method is taking the place of that.

You are correct about do\_copy(). After calling create(), uvm\_object::clone(), calls uvm\_object::copy, which then calls the virtual do\_copy().

---

<div class="post-metadata">

**Author:** ![svq](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@svq](https://verificationacademy.com/forums/u/svq)\
**Post date:** [January 5, 2020, 7:13pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/17 "2020-01-05T19:13:04Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/17):_

Thanks Dave for the explanation. Makes sense now

---

<div class="post-metadata">

**Author:** ![Have\_A\_Doubt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Have\_A\_Doubt](https://verificationacademy.com/forums/u/Have_A_Doubt)\
**Post date:** [September 25, 2021, 5:20pm UTC](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/18 "2021-09-25T17:20:47Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/importance-of-the-clone-method/25472/18):_

Dave , I believe there’s a few typos

```verilog

class ex_base ;   
  should be 
class ex_base extends base ; // Should extend base 
 int p2;
  function void copy(base orig);
    super.copy(orig); // Arg. name should be 'orig'
    this.p2 = orig.p2;
  endfunction

```
