What is singleton class in system verilog

Companies Related Questions, System Verilog 0 Comments

so question what is singleton class in system verilog

as its name suggest Singleton -> it will be only once ….

what is once , it is one object creation next time it will return handle of first object

So  lets see how it done through program

 

 

 

// Singleton example
// https://testbench4u.com

class single;

int i;

static single obj;

local function new(int i=0);

this.i= i;

endfunction

function void display();

$display(“value of int i= %d”, i);

endfunction

static function single obj_create(int i=0);

if (obj == null) begin
obj =new(i);
end
return obj;

endfunction
endclass
program main;

single obj1;

single obj2;

initial begin
obj1 = single::obj_create ( );
obj2 = single::obj_create ( );
obj1.i=5;

obj1.display();

obj2.i=51;

obj2.display();
end

endprogram

o/p

value of int i= 5
value of int i= 51

in above example we can see through static keywords function is made of that type class

its see if the obj is null then create it or else return the handle  of single

 

Leave a Reply