I would like to confirm my understanding of local and protected in a SV class.
local variable - this is local to the class where it is specified. Neither it is available in the derived class or module that instantiates this class
protected variable - this is not available in the module that instantiates but is available to the derived class
I assume my understanding is correct.
Another question, is it possible to change the access type of a variable in the derived class, that is defined in the base class.
To elaborate, x variable is declared as protected in base class. Can it be changed to local in derived class closing its access for the subsequent derived classes?
In reply to verif_learner:
Yes, excecpt that the rules applies to anywhere outside the class, not just other modules that instantiate this class.
No, you cannot change the access type.
Hi All,
There is very simple example of access type overriding in derived class.
class base_class;
bit public_flag;
function new ();
endfunction
endclass
class child1 extends base_class;
local bit public_flag; // Change type to local
function new();
super.new();
endfunction
endclass
class child1_child extends child1;
function new ();
super.new();
endfunction
endclass
class child2 extends base_class; // No access type change for public_flag
function new ();
super.new();
endfunction
endclass
class test;
child1_child test1;
child2 test2;
function test ;
test1.public_flag =1 ; // Try to override local
test2.public_flag = 1; // Try to override public
endfunction
endclass
When compile with Questasim 2021.3, It’s generated log like :
** Error (suppressible): (vlog-8688) testbench.sv(33): Illegal access to local member public_flag.
Full name of member: testbench_sv_unit::child1::public_flag
Full name of calling scope: testbench_sv_unit::test
So , end of the day we can change access type in derived class.
I just want to make it clear. Is Dave’s answer still valid or up to date ?
In reply to yusuf_eren:
Actually what you showed is not recommended basically you created a new variable with the exact same name in the child1 class you can see this if you print the object, what dave described still valid.
In reply to yusuf_eren:
Actually what you showed is not recommended basically you created a new variable with the exact same name in the child1 class you can see this if you print the object, what dave described still valid.