Dynamic casting

I would like to understand how dynamic casting is updating the argument value for the below task:


//somewhere in class :
variable_item_name a1;
task_name(a1);

task task_name(variable_item_name a);
variable_item_name b;
$cast(temp,a);
if(temp.enable == 1) begin
temp.val1 = $urandom_range(10);
temp.val2 = $urandom(15);
end
endtask: task_name

My observation is: in the task, if variable temp is randomized, the same randomized values are reflected in input argument of the task as well. Is this behavior expected?

In reply to soumyarayaprolu:
Yes.

In reply to dave_59:

Thank you! To help me understand better, can you let me know how different is the it with regards to below task:

task task_name(variable_item_name a);
if(a.enable == 1) begin
a.val1 = $urandom_range(10);
a.val2 = $urandom(15);
end
endtask: task_name

If essence is same what is the purpose of casting in the above task?

In reply to soumyarayaprolu:
You never explained what ‘variable_item_name’ was. I’m going to assume it is a class type. You never showed the declaration of ‘temp’. I’m going to assume it is a class variable of a type that has been extended from ‘class_variable_name’.

When you make an assignment to a class variable; from a direct assignment, a dynamic $cast, or passing an argument to a task/function, you are only copying a handle to a class object, not copying the object itself.

Please see my course on SystemVerilog OOP, especially the first session.