队列同样可以保存类对象,这在验证环境中是非常有用的,下面是一个例子:
class animals;
string sname;
int i1;
function new (string name="UN");
sname = name;
i1++;
endfunction
endclass
module tb;
// queue of class type 'animals'
animals alist [$];
initial begin
animals f, f2; //declare two variables of type animals
// Create a new class object 'f' and push into the queue
f = new ("lion");
alist.push_front (f);
// Create another class object 'f2'and push into the queue
f2 = new ("tiger");
alist.push_back (f2);
// Iterate through queue and access each class object
foreach (alist[i]) begin
$display ("alist[%0d] = %s", i, alist[i].sname);
$display ("alist[%0d] = %p", i, alist[i]);
end
// Simply display the whole queue
$display ("alist = %p", alist);
end
endmodule
仿真log:
# KERNEL: alist[0] = lion
# KERNEL: alist[0] = '{sname:"lion", i1:1}
# KERNEL: alist[1] = tiger
# KERNEL: alist[1] = '{sname:"tiger", i1:1}
# KERNEL: alist = '{'{sname:"lion", i1:1}, '{sname:"tiger", i1:1}}
该示例声明了一个名为“animals”的类,字符串“name”初始化为“UN”。
声明了两个“animals”类型的对象" f "和“f2”。
创建“animals”类型的对象队列“alist”。
分别实例化两个对象“f”和“f2”,然后push到队列中。