As shown in the following questions:
class Transaction
logic [31:0] addr = 'h10;
logic [31:0] crc,data[8];
function new(logic [31:0] a =3, d =5);
addr = a;
foreach (data[i])
data[i] = d;
endfunction
endclass
initial begin
Transaction tr,tr1;
tr = new(10);
tr1 = new();
end
To understand the process of creating objects by new (), first of all, new (10) opens up space according to the variable types in the class, addr, crc, and data, then addr is assigned the value 16. After that, we pass the parameter 10 into the function, and the addr value is overwritten to 10.
When the new () function is called, no parameters are passed from the outside, so the addr value is assigned 3.
Object creation and destruction:
class word
byte nb[];
function new(int n);
nb = new[n]; //动态数组空间开辟
endfunction
endclass
initial begin : initial_1
word wd;
for(int i=1;i<=4;i++) wd = new(i); //创建了4个对象
end
initial begin : initial_2
#1ps
$display("How many Bytes are allocated for word instance?")
end
If the space required for wd = new (1) is 1B, then the space is created after execution:
After wd is executed, the handle points to the fourth object, 4B. Since the initial variable is static, if the initial execution is completed, the variable is still there.
class Transaction
static intcount= 0;
int id;
function new();
id = count++;
endfunction
endclass
Transaction t1,t2;
initial begin
t1 = new();
t2 = new();
$display("Second id=%d, count = %d",t2.id,t2.count);