我想做这样的事情,但它会导致错误:
class MyPacket;
function MyPacket not_fun_ction();
$display("not fun");
return this;
endfunction
endclass
module testbench;
MyPacket temp;
initial
begin
temp = MyPacket::new().not_fun_ction().not_fun_ction();
end
endmodule
我知道,我无法指定构造函数的返回值(link)。 但构造函数返回对象引用,因为这个赋值有效:
temp = MyPacket::new();
。为什么我不能只对返回的引用调用成员方法?在大多数其他面向对象的编程语言中,这应该是有效的。
(我知道,当我将其写为两个单独的语句时,它会起作用:
temp = MyPacket::new(); temp.not_fun_ction().not_fun_ction();
,但我想将其写在一个语句中。)
但是构造函数返回对象引用——错误。它可以在 C++ 中运行,但不能在 verilog 中运行。
LRM:新操作被定义为一个没有返回类型的函数,并且像任何其他函数一样,它应该是 非阻塞。即使 new 没有指定返回类型,赋值的左侧 确定返回类型。
因此,它是一种特殊类型的函数,仅适用于直接赋值。以下内容将起作用:
initial
begin
temp = new;
temp.not_fun_ction().not_fun_ction();
end