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.

What is the benefit of using virtual in systemverilog?

Status
Not open for further replies.

u24c02

Advanced Member level 1
Advanced Member level 1
Joined
May 8, 2012
Messages
404
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
4,101
What is the benefit of using polymorphism in systemverilog?

I'd like to know "practically" the benefit of virtual used in systemverilog.

Here is simple example,

Code:
   program main();
    
    class A;
    virtual task disp();
    $display("This is a A class");
    endtask
    
    class A_1 extend A;
    task disp();
    $display("This is a A_1 class");
    endtask
    endclass
    
    class A_2 extend A;
    task disp();
    $display("This is a A_2 class");
    endtask
    endclass

    class A_3 extend A;
    task disp();
    $display("This is a A_3 class");
    endtask
    endclass
    
    
    ...


    A a;
    A_1 a_1;    
    A_2 a_2;    
    A_3 a_3;    

    initial begin
    
    a = new();
    a_1 = new();
    a_2 = new();
    a_3 = new();

    a.disp();
    a = a_1;
    a.disp();
    a = a_2;
    a.disp();
    a = a_3;
    a.disp();

    end

   
    endprogram


As I understand from here, virtual is just can make overriding to another function. and If I want to use polymorphism then I have to use the 'virtual'.

BTW, I can't find any other benefits that using the polymorphism
Does anyone let me know what is the benefit of using the polymorphism ?
 

Its not as simple as that.
Making a task virtual means that the version at the bottom of the class heirarchy gets called. If a task isnt virtual, it calls the one in the parent class. It only needs to be declared virtual once, at the top of the heirarchy.

In adition, it means you could define some behaviour in A.disp() that is common to all classes, which you call in the child classes:

Code:
class A_3 extend A;
    task disp();
      super.disp();  //do something common to all classes
      $display("This is a A_3 class");
    endtask
endclass

By default the new() function is virtual, and all calls to new() always call super.new() before any other code in the constructor is executed.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top