Class variables in SystemVerilog are references or descriptors. Instances are created only when the new keyword is used.
So, in your example, obj1 and obj_array[i] both refer (or point) to the same instance.
By default, function parameters in SystemVerilog are passed by value. However, class descriptors are treated as values, so any class that you pass to a function is effectively passed by reference.
There is a built-in mechanism in the language to make a shallow copy when initializing a class object.
Packet p1; Packet p2; p1 = new; p2 = new p1;
It makes a shallow copy. For objects, only pens are copied!
This is explained in the examples in chapter 8.11 of IEEE 1800-2009.
The var keyword has nothing to do with the behavior you see. In fact, I have not even seen or used var . According to LRM, this allows you to omit the type when declaring a variable. Your code specifies a type (cls_obj), so I donβt think its presence is doing anything.
source share