module virtual_method;
// this example is referred to the lab1 class inheritance
// the purpose is to learn the convenience of the virtual method
class trans;
bit[31:0] data;
int pkt_id;
int data_nidles;
int pkt_nidles;
bit rsp;
string name;
virtual function trans clone(trans t = null);
if (t == null)
t = new();
t.data = data;
t.pkt_id = pkt_id;
t.data_nidles = data_nidles;
t.pkt_nidles= pkt_nidles;
t.rsp = rsp;
return t;
endfunction
virtual function print_f(int i, name = "tr");
$display("this is trans class i = %d,name = %s",i,name);
endfunction
endclass
class ch_trans extends trans;
int ch_id;
string name;
virtual function trans clone(trans t = null);
ch_trans ct;
if (t == null)
ct = new();
else
void'($cast(ct,t));
ct.ch_id = ch_id;
return ct;
endfunction
virtual function print_f(int i, name = "ch_trans");
$display("this is ch_trans class i = %d name =%s ",i,name);
endfunction
endclass
initial begin
trans t1,t2;
ch_trans ct1,ct2;
ct1 = new();
ct1.pkt_id = 2;
ct1.ch_id = 3;
$display("ct1 = %p",ct1);
t1 = ct1;
t1.print_f(7);
end
endmodule
我很困惑为什么 t1.print(7) 函数打印 name = "tr" 而不是 "ch_trans"。
我在VCS调试发现t1.print_f(7)调用子类print_f函数没有调用类trans print_f函数
我认为这个打印“ch_trans”;但它没有。
此行为在当前的 LRM 中是一个不明确的。第 8.20 节是关于参数默认值的:
没有必要有匹配的默认表达式,但默认的存在应该匹配。
问题是它没有说明应该采用哪个默认值,大多数工具都选择了由被引用的类变量的类型指定的默认值。在这种情况下,
t1
定义为类类型trans
.