cyboman
Member level 4
I'm trying to understand the meaning of virtual methods in SystemVerilog. Say we have the following code
What method will be called in case 3, the one from Transaction or the one from BadTransaction? I'm suspecting since calc_something() in BadTransaction is not virtual, calc_something() from Transaction class will be called. What I'm trying to understand is for polymorphism to work, do both methods need to be virtual or only the in the base class?
I know this looks like a homework assignment but it is not. This is my personal curiosity. I would have tested it myself, but unfortunately right now, I don't have an access to the simulator.
Any help is appreciated.
Code:
class Transaction;
...
some attributes
...
virtual function calc_something();
...
calculating something
...
endfunction
...
endclass
class BadTransaction extends Transaction;
...
some other attributes
...
function calc_something();
...
calculate something
...
endfunction
...
endclass
Transaction tr;
BadTransaction badTr;
initial begin
// case 1
tr = new();
tr.calc_something(); // Calls Transaction::calc_something
// case 2
badTr = new();
badTr.calc_something(); // Calls BadTransaction::calc_something
// case 3
tr = badTr; // base handle points extended object
tr.calc_something(); // What method will be called, the one from Transaction class or the one from BadTransaction?
end
What method will be called in case 3, the one from Transaction or the one from BadTransaction? I'm suspecting since calc_something() in BadTransaction is not virtual, calc_something() from Transaction class will be called. What I'm trying to understand is for polymorphism to work, do both methods need to be virtual or only the in the base class?
I know this looks like a homework assignment but it is not. This is my personal curiosity. I would have tested it myself, but unfortunately right now, I don't have an access to the simulator.
Any help is appreciated.