Use of $cast to copy values

I used the following code to copy data in the object “item” to “pkt”

$cast(pkt,item);

Then I have performed an operation on “pkt” object. But I observed that same changes are appearing on “item” also when I printed the content.

But this is not happening when I used copy() method that is available in UVM.

pkt.copy(item);

Why it happens so?

In reply to bachan21:

Can you share more test bench code on how these objects are declared and used? are they both pointing to the same object?

$cast syntax is as follows
$cast(destination,source)

It is both a type check and also an handle assignment from the source to the destination but not the other way.

In reply to hkc:

Hello,

I attach further piece of code here

`uvm_info(get_full_name(),{"\nOBSERVED OUTPUTS ::\n",item.convert2string()},UVM_LOW)
	
pkt = idct_sequence_item::type_id::create("PKT");
		
$cast(pkt,item);
//pkt.copy(item);
				
//Calculating expected outputs
pkt = M0.idct_process(pkt);
`uvm_info(get_name(),{"\nEXPECTED OUTPUTS ::\n",pkt.convert2string()},UVM_LOW)

After calling idct_process(pkt), both “pkt” and “item” has same interval property values which is undesired. So I used copy (do_copy) method to solve this issue so that process done on “pkt” is not reflected on “item” object.

In reply to bachan21:

Could you please sho the definition of ‘item’.

In reply to bachan21:

$cast(pkt,item) is copying the handle, not the object. After executing this statement, put and item are two class variables containing the same class handle.

See A Short Class on SystemVerilog Classes - Verification Horizons