Deal All,
I’m trying to understand virtual function functionality with a simple example.
// Code your testbench here
// or browse Examples
class Base;
int a =10;
virtual function void method1();
$display("Base: Method1");
method2();
endfunction
virtual function void method2();
$display("Base: Method2");
endfunction
virtual function void method3();
$display("Base: Method3");
method2();
endfunction
endclass
class Derived extends Base;
int b=11;
function void method1();
$display("Derived: Method1");
method2();
endfunction
function void method2();
$display("Derived: Method2");
endfunction
function void method3();
$display("Derived: Method3");
method2();
endfunction
endclass
class Derived_two extends Derived;
int c=12;
function void method1();
$display("Derived_two: Method1");
method2();
endfunction
function void method2();
$display("Derived_two: Method2");
endfunction
function void method3();
$display("Derived_two: Method3");
method2();
endfunction
endclass
module test;
initial
begin
Base base;
Derived derived;
Derived_two derived_two;
derived_two = new();
base = derived_two;
$display("Test2 ");
base.method1();
$display("----");
base.method2();
$display("----");
base.method3();
$display("Test3 ");
$display(base.a);
$display("----");
$display(base.b);
$display("----");
$display(base.c);
end
endmodule
From above example , I learn that base can control the sub-classes by up-casting. this is very handy by up-casting.
But. I’ve got 2 question about properties and down-casting.
- Even I declared methods as virtual, I can’t access another properties except declared class
$display(base.b);
|
: *E,NOTCLM (testbench.sv,91|16): b is not a class item.
$display(base.c);
|
: *E,NOTCLM (testbench.sv,93|16): c is not a class item.
So I’m wondering that How do we access properties within virtual and Up-Casting topology?
- Why do we do Down-casting?