Are class object handles always passed by reference when used are class method arguments?
Or does one need to qualify the formal argument as ref, or perhaps inout?
Concrete example I am working on: I have class function
function int to_gp(uvm_tlm_generic_payload gp)
which maps my (transaction) class attributes to generic payload.
I will generally want to call super.to_gp(gp) where my class extends from some base class that already implements to_gp for its base class attributes.
Let’s assume I do a gp = new in the to_gp function of that base class, then set the gp object’s data and data_length etc. I would now expect that in the derived class the new gp object survives the supr.to_gp call so that I can access its data and other contents. But I get a null object access.
I have tried with ref but run into some issues from which I have yet to conclude what;s going on, and I am just trying now inout but that seems counter intuitive.
I am firing off this question since I actually would like to know from an expert - I did not readily find an answer in the LRM.
Hi Dave,
From the response and embedded link I get that I should not need to use ref for my case as I described. Interestingly though, when not using it I get a null object, when using it, it appears to work. Aside, this is with VCS.
In the other response it does state that you almost never need to use ref with class object handles. So I thought to. Any my use case does not seem to be a case for which this is an exception, right?
Hard to answer without seeing more code, but if you are constructing a class handle inside a function that you want to pass out as a function argument, use an output direction, not a ref.
No the class object handles are not always passed by reference. Because they themselves are reference variables. They hold the reference of the objects allocated. everytime new() is called a handle which either initially points to null or another object, will start pointing to the newly dynamically constructed object.
So declaring a handle without ref in the function argument means, a handle which has an unique address always pointing to null untill the method was called upon with the object a handle pointing was passed. when you call new() on that handle inside the function, will only reflect within the function and not to the handle which was passed.Class object handles are not always passed by reference. A class handle is itself a reference variable, it stores a reference to a dynamically allocated object.
When new() is called, the handle is made to point to the newly constructed object. If the handle was previously null, it starts pointing to the new object; if it was already pointing to another object, it is updated to point to the new object instead.
When a class handle is passed to a function without ref, the handle itself is passed by value. In other words, the function receives its own local handle variable, containing a copy of the reference stored in the caller’s handle.
Therefore, if the caller’s handle is null, the function’s local handle initially also contains null. If the function then executes: handle = new();
the new() operation changes only the local handle inside the function. The caller’s handle is not changed, because the two handles are separate variables.
However, if the handle is passed using ref, the function receives a reference to the actual handle variable itself. Therefore, when the function executes: handle = new(); the caller’s handle is also updated to point to the newly allocated object.