interface Int(); class t1; integer a[string]; //REQ-EXT-10007 function new; //class constructor body need be modified according to REQ-EXT-10007 a["a"] =40; a["b"] =60; a["c"] =80; endfunction endclass class t2; integer b[string]; //REQ-EXT-10007 function new; //class constructor body need be modified according to REQ-EXT-10007 b["aa"] =44; b["bb"] =66; b["cc"] =88; endfunction endclass t1 inst1 =new; //REQ-EXT-10501 t2 inst2 =new; //REQ-EXT-10501 endinterface module top; Int in(); virtual Int testint =in; integer array[string]; //data type of array need be modified according to REQ-EXT-10007 initial begin #1; array =testint.inst1.a; $display("testint.inst1.a =%p", testint.inst1.a); //CHECK-001:'{"a":40, "b":60, "c":80} $display("array = %p", array); //CHECK-002:'{"a":40, "b":60, "c":80} #2; array =testint.inst2.b; $display("testint.inst2.b =%p", testint.inst2.b); //CHECK-003:'{"aa":44, "bb":66,"cc":88} $display("array =%p", array); //CHECK-004:'{"aa":44, "bb":66,"cc":88} end 根据REQ-EXT-10501, 这段代码可以如何修改,修改之后仍然可以跑通呢?
时间: 2024-03-04 15:49:17 浏览: 118
C ++仅标头的HTTP / HTTPS服务器和客户端库-C/C++开发
根据REQ-EXT-10501的要求,可以将 t1 和 t2 类修改为继承自一个基类,然后在 Int 接口中定义一个函数,返回基类的指针。修改后的代码如下:
```
interface Int();
class base;
function new(string name);
$display("Creating instance of class %s", name);
endfunction
endclass
class t1 extends base;
integer a[string]; //REQ-EXT-10007
function new;
super.new("t1");
a["a"] = 40;
a["b"] = 60;
a["c"] = 80;
endfunction
endclass
class t2 extends base;
integer b[string]; //REQ-EXT-10007
function new;
super.new("t2");
b["aa"] = 44;
b["bb"] = 66;
b["cc"] = 88;
endfunction
endclass
base get_base(string name);
endinterface
module top;
Int in();
virtual Int testint = in;
integer array[string]; //data type of array need be modified according to REQ-EXT-10007
initial begin
#1;
testint.get_base("t1").a = array;
$display("testint.inst1.a = %p", testint.get_base("t1").a); //CHECK-001:'{"a":40, "b":60, "c":80}
$display("array = %p", array); //CHECK-002:'{"a":40, "b":60, "c":80}
#2;
testint.get_base("t2").b = array;
$display("testint.inst2.b = %p", testint.get_base("t2").b); //CHECK-003:'{"aa":44, "bb":66,"cc":88}
$display("array = %p", array); //CHECK-004:'{"aa":44, "bb":66,"cc":88}
end
endmodule
```
在修改后的代码中,t1 和 t2 类都继承自一个名为 base 的基类,并在其中定义了一个带参数的构造函数,用于输出类的名称。在 Int 接口中定义了一个 get_base 函数,根据传入的参数返回相应的基类指针。在初始化代码中,通过调用 get_base 函数来获取相应的基类指针,然后将其数组成员赋值给数组变量 array,实现了原有代码的功能。
阅读全文