I cannot quite understand why these two cases yield different results:
- In extended class, virtual method is defined
- In extended class, virtual method is NOT defined.
In detail, the situation and source code is shown below. Points here are:
- Object handler’s type is BaseClass, which points to ExtendClass (i.e. casting is used)
- In the method, class variable (A, B) is referred.
Method::printA()/printB() displaying class variable(int A, B) are declared in the class.
As the contents in these functions are the same for both Base and Extended Class,
I omit overriding printA(), which gives different results compared to printB().
module tb();
class BasePacket;
int A; //1;
int B; // 2;
function new(int A=1, int B=2);
this.A=A;
this.B=B;
endfunction
virtual function void printA;
$display("[%m] A is %0d", A);
endfunction
virtual function void printB;
$display("[%m] B is %0d", B);
endfunction : printB
endclass : BasePacket
class My_Packet extends BasePacket;
int A; // 3
int B; // 4
function new(int A=3, int B=4);
this.A=A;
this.B=B;
endfunction
/* -----\/----- EXCLUDED -----\/-----
virtual function void printA;
$display("A is %0d", A);
endfunction
-----/\----- EXCLUDED -----/\----- */
virtual function void printB;
$display("[%m] B is %0d", B);
endfunction : printB
endclass : My_Packet
initial begin
BasePacket P1;
My_Packet P2 = new;
P1 = P2; // P1 has a handle to a My_packet object
P1.printA; // A is 1
P1.printB; // B is 4
end
endmodule // tb
Thanks