Sunteți pe pagina 1din 3

system verilog - What is the purpose the 'new' and 'virtual' in systemverilo...

1 of 3

http://stackoverflow.com/questions/35353329/what-is-the-purpose-the-n...

sign up

log in tour

help

x Dismiss

Join the Stack Overflow Community


Stack Overflow is a community of 6.4 million
programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What is the purpose the 'new' and 'virtual' in systemverilog?


Now I'm trying to study about systemverilog. I found the following code that are make confuse me.
Test1.
class A ;
task disp();
$display(" This is class A ");
endtask
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_a.disp();
my_a = my_ea;
my_a.disp();
end
endprogram

Test2.
class A ;
virtual task disp ();
$display(" This is class A ");
endtask
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_a = new();
my_a.disp();
my_ea = new();
my_a = my_ea;
my_a.disp();
end
endprogram

I have question at the above test1 code, you can see that the 'new' is nothing( there is nothing about the new). But the code can compile and
run I can't understand. How can it does compile and run?
Also in the test2, you can see the 'virtual' keyword. But I can not understand about the reason of using the 'virtual'. Of course, I have read them
related. So does anyone let me know why do we use virtual.

12-Dec-16 9:31 PM

system verilog - What is the purpose the 'new' and 'virtual' in systemverilo...

2 of 3

http://stackoverflow.com/questions/35353329/what-is-the-purpose-the-n...

system-verilog
edited Feb 12 at 2:06

asked Feb 12 at 1:33

Greg
10.5k

bural
5

20

41

18

1 Answer

The new keyword is a constructor, it creates the object. Since


the default constructor:

new

is not defined it is inferring

function new();
endfunction

Objects must be constructed before you call any of there methods. Test1 should through a null
pointer error because you call an object's method that hasn't been constructed.
The virtual keyword and concept is the same in C++, Java, etc. There are plenty of
explanations of this already answered on the virtual topic and polymorphism, such as : Why
do we need Virtual Methods in C++?
In a nutshell a parent handle pointing to a child object can execute the object's method if it is
virtual. Best way understant this is the create a class and child class that has both a virtual and
non-virtual methods. Example:
module main ;
class A ;
function void disp ();
$display(" Non-Virtual from A ");
endfunction
virtual function void vdisp ();
$display(" Virtual from A ");
endfunction
endclass
class EA extends A ;
function void disp ();
$display(" Non-Virtual from EA ");
endfunction
virtual function void vdisp ();
$display(" Virtual from EA ");
endfunction
endclass
function(A a);
a.disp();
a.vdisp();
endfunction
EA my_ea;
A my_a;
initial
begin
my_a = new();
my_ea = new();
disp(my_a);
disp(my_ea);
my_a = my_ea;
disp(my_a);
end
endprogram
answered Feb 12 at 2:05

Greg
10.5k

20

41

12-Dec-16 9:31 PM

system verilog - What is the purpose the 'new' and 'virtual' in systemverilo...

3 of 3

http://stackoverflow.com/questions/35353329/what-is-the-purpose-the-n...

Thanks Greg, but test1 compiled and elab and simulation well. I don't knownthe reason.. test1's output
display like this this is class A ...this is class A. Is this something wrong that I can't see? bural Feb 12
at 2:41
There is nothing wrong with test1 and as the virtual keyword is not used in disp() method of class A, it
doesn't allow EA to override it and hence second time also it prints "this is class A". H.Modh Feb 12 at
3:23
You can refer 5th chapter of "systemverilog for verification chris spear 3rd edition" for quick reference and
then look into LRM for more detailed explanation. H.Modh Feb 12 at 3:26
@H.Modh Thanks, When I use virtual keyword to task of the first class A, then i got an error.
E.TRNULLID: NULL pointer dereference. bural Feb 12 at 4:22
1

You haven't created an object for class EA. H.Modh Feb 12 at 5:11

12-Dec-16 9:31 PM

S-ar putea să vă placă și