[SOLVED] SystemVerilog virtual methods

Status
Not open for further replies.

cyboman

Member level 4
Joined
Mar 9, 2010
Messages
71
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
USA
Visit site
Activity points
1,808
I'm trying to understand the meaning of virtual methods in SystemVerilog. Say we have the following code

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.
 

Remember: "once virtual - always virtual"

You may have an excuse in not having convenient access to a simulator, but there is no longer an excuse for not have access to the LRM.. See https://go.mentor.com/get-1800.
8.20 Virtual methods said:
A virtual method may override a non-virtual method, but once a method has been identified as virtual, it
shall remain virtual in any subclass that overrides it. In that case, the virtual keyword may be used in later
declarations, but is not required.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…