Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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
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.
 

@dave_59
Thanks for the help. I appreciate your help.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top