在 SystemVerilog 中,我有一个基类 A
和派生类 B
、C
、D
。我想创建一个 A
类型的数组,其中包含 B
、C
、D
的句柄。有没有比下面我丑陋的解决方案更简洁的方法?
module test;
A arr[3];
B b;
C c;
D d;
initial begin
b = new();
c = new();
d = new();
arr[0] = b;
arr[1] = c;
arr[2] = d;
end
endmodule
最佳答案
如果你的意思是不使用中间类变量,你可以这样做
arr[0] = B::new();
这是 SystemVerilog 的一个相对较新的功能,一些工具尚不支持此功能。为了解决这个问题,您可以创建一个静态 create
方法来为您调用构造函数,这样您就可以在函数的参数等位置创建类对象,而无需声明中间变量。
class B;
static function C create;
create = new;
endfunction
endclass
arr[1] = C::create();
somefunction(C::create()); // C::new() would not work here
如果您使用 UVM 库,则可以免费获得该库
class C extends uvm_object;
`uvm_object_utils(C)
...
endclass
arr[3] = C::type_id::create();
关于system-verilog - 创建子句柄数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333669/