Inheritance 为什么在systemverilog中继承常量变量不起作用
以下代码有什么问题Inheritance 为什么在systemverilog中继承常量变量不起作用,inheritance,compiler-errors,system-verilog,Inheritance,Compiler Errors,System Verilog,以下代码有什么问题 program test; class a; const int i; endclass : a class b extends a; function new(); i = 10; endfunction : new endclass : b class c extends a; function new(); i = 100; endfunction : new endclass : c in
program test;
class a;
const int i;
endclass : a
class b extends a;
function new();
i = 10;
endfunction : new
endclass : b
class c extends a;
function new();
i = 100;
endfunction : new
endclass : c
initial begin
b bo = new();
c co = new();
$display(bo.i, co.i);
end
endprogram : test
我得到以下编译错误
实例常量的初始化无效:“i”无法初始化
在构造函数中不止一次。有可能重新初始化
at语句:this.i=10;上一个地址:test.sv,9源信息:
这个。i=10
在类对象的构造过程中,只能对
const
变量进行一次赋值。它必须作为其声明的一部分,或者在其相应的构造函数方法中赋值。即使您没有在类a
中编写显式构造函数方法,SystemVerilog也会为您创建隐式构造函数。两者都没有,因此其初始值为0。重写实例常量值的唯一方法是将其作为构造函数参数或参数化传递
class a;
const int i;
function new(int aa = 10);
i = aa;
endfunction
endclass : a
class b extends a;
function new(int aa=100);
super.new(aa); // super.new(); is implicitly added if you don't
endfunction : new
endclass : b
class c extends b;
function new();
super.new(500); // super.new(); is implicitly added if you don't
endfunction : new
endclass : b
还要注意,当变量在过程代码中隐式声明为静态变量时,初始化变量是非法的。您应该将它们移到开始/结束块之外,或者使用静态
或自动
关键字添加显式生存期
b bo = new();
initial begin
static c co = new();
$display(bo.i, co.i);
end
声明在过程循环中会有很大的不同 我的问题不同。这两个继承的类不是每个常量变量都有一个副本吗谢谢你的澄清。我现在让它工作了。但是,我不理解您对程序
->模块
所做的编辑以及静态
或自动
的内容。我只使用了super.new(10)
和super.new(100)