是否可以在分配语句之外的SystemVerilog中调用new?



这就是我在SystemVerilog中如何调用new

class A;
endclass
A a = new();

但有时,我不需要本地对象,我只想将其直接发送到一个接受A的函数。有没有办法在这里显式调用新函数:

function use_a(A obj);
endfunction
use_a(new());  // <--- How to write this call to specify which new to call?
use_a(A::new());   // <--- new not expected here :(

不幸的是,SystemVerilog的语法不允许这样做。特殊new方法不是静态方法,由于类内存管理的定义方式,类句柄必须存在于某个变量中。您可以通过将new包装在静态方法周围来解决此问题:

class A;
static function A create();
create = new();
endfunction
endclass
...
use_a(A::create());

顺便说一句,UVM 在 BCL 中具有create方法,您几乎不需要直接调用new()

最新更新