Multiple constructors in System verilog

A

Ashish Dobhal

Guest
Can we have multiple constructors in a single class in System Verilog?
E.g.,

module test;

class A;
int i;
function new();
i=0;
endfunction

function new(input int a);
i=a;
endfunction

endclass

A a1;
initial
begin

a1=new(2);
$display("value is :%d",a1.i);
end

endmodule
 
Ashish Dobhal wrote:
Can we have multiple constructors in a single class in System Verilog?
E.g.,

module test;

class A;
int i;
function new();
i=0;
endfunction

function new(input int a);
i=a;
endfunction

endclass

A a1;
initial
begin

a1=new(2);
$display("value is :%d",a1.i);
end

endmodule
No, you can not have two methods of the same name. What
you can have, however, are two functions, say new1() and
new2, and you can use one or the other as per your
requirements as constructor for a particular instance.

--
SystemVerilog, DPI, Verilog PLI and all other good stuffs.
Project VeriPage: <URL: http://www.project-veripage.co­­m/>
Get information on new articles:
<URL: http://www.project-veripage.co­­m/list/?p=subscribe&id=1>
 

Welcome to EDABoard.com

Sponsor

Back
Top