Why isn’t this re-declaration of arr array flagged as an error?
Doesn’t my_class2 already contain member variable arr?
class my_class;
bit [7:0] arr [10];
endclass
class my_class2 extends my_class;
bit [7:0] arr [15];
endclass
module tb;
my_class obj;
my_class2 obj2;
initial begin
obj = new();
obj2 = new();
$display("arr size in obj is %0d", $size(obj.arr));
$display("arr size in obj2 is %0d", $size(obj2.arr));
end
endmodule : tb
Why should it be it simply resides inside of a different namespace and can still be accessed.
Like shown in the following example:
class my_class;
bit [7:0] arr [10];
endclass
class my_class2 extends my_class;
bit [7:0] arr [15];
function ret_super_arr ();
$display("arr size in super.arr is %0d", $size(super.arr));
endfunction
endclass
module tb;
my_class obj;
my_class2 obj2;
initial begin
obj = new();
obj2 = new();
$display("arr size in obj is %0d", $size(obj.arr));
$display("arr size in obj2 is %0d", $size(obj2.arr));
obj2.ret_super_arr();
end
endmodule : tb
WARNING: although it is possible to override class members, it creates many problems and is not recommended.Most of the time it is done by mistake when copy/pasting a base class into a derived class and inadvertently copying the member variables.
One such example problem is with constraints. When you override a class member, the constraints specified in the base class do not get applied to the overridden member.
class A;
rand int my_int;
constraint c {my_int > 5;}
endclass
class B extends A;
rand int my_int;
constraint c {my_int < 10;}
endclass
Some people incorrectly assume that my_int is constrained to be 6,7,8, or 9.