class A; virtualfunctionint dis(string str="A"); $display("this is %s",str); dis=1; endfunction endclass
class B extends A; functionvoid dis(); // 只有函数名相同 $display("this is B"); endfunction endclass A a; B b; initialbegin b=new; b.dis(); b.dis("B"); //在调用的时候,SV会把B的dis当作A中dis的override来处理,句柄b调用的是B中的dis。 end // 报错: Error-[SV-IRT] Incompatible return types ./svt.sv, 108 svt, "dis" Definition of classfunction 'A::dis' does not have the same returntype as mentioned in the declaration at: "./svt.sv", 97.
Error-[TMAFTC] Too many arguments to function/task call ./svt.sv, 116 "b.dis("B")" The above function/task call is done with more arguments than needed.
// 任务被函数重载了,会出错 programautomatic test; class A; virtualtask dis(string str=""); $display("this is A"); endtask endclass
class B extends A; functionvoid dis(string str=""); $display("this is B"); endfunction endclass A a; B b; initialbegin b=new; b.dis(); a=b; a.dis(); a.dis("A"); end endprogram // 报错: Error-[SV-ICMO] Illegal class method override ./svt.sv, 100 Virtual task 'dis' cannot be overridden with a function. Virtual method declared at "./svt.sv", 100 Overriden at "./svt.sv", 111
4. new可以被override吗
之前面试被问到new函数可以重载吗?
在UVM UG1.1 P62页顶部提到”there are limitations on overriding new() in object-oriented language such as System Verilog.”这好像说明了new是可以重载的,但是有一些限制。