Hi,
I would like to know if there is any way to override non-virtual method in a class?
You can override any method of a class, the question is really how to access the non-virtual method, or should I have made the method virtual in the first place.
With a virtual method, you always access the overridden method regardless of the class variable used to reference it. When you have a non-virtual method, you must use a class variable with a class type that has the overridden method defined. You have a few choices, but realize none of them may work for you and you need to go back and make the method virtual.
If the code knows the class has been overridden, you need to put the handle to extended class object in an extended class variable. You do this by constructing the object and keeping it an extended class variable, or using $cast(ext_h,base_h) to move the handle back into an extended class variable.
Another technique is to call a virtual method that gets you into the scope of the extended class, and call the non-virtual method. This is how the clone()/copy() methods work. clone() is a virtual method that constructs a new object receiving the copy, then calls the non-virtual copy() method to perform the copy to the new object.
In reply to dave_59:
Hi Dave,
Thank you for the reponse.
Let me show you with an example what I want to achieve.
class square;
int area;
function sq(int s)
area = s*s;
$display(“s = %0d area = %0d”,s,area);
endfunction
endclass
class square_ext extends square;
int perimeter;
function sq(int s)
area = ss;
perimeter = area2;
$display(“s = %0d area = %0d perimeter = %0d”,s,area,perimeter);
endfunction
endclass
In a top module;
square s;
square_ext s_ext = new();
$cast(s,s_ext);
s.sq(4);//Call the parent function (as expected as the function is non-virtual in the parent class)
//I want the child function to be invoked.
How can I override the non-virtual function “sq” ?
In reply to Prathiksha:
Please don’t use the terms parent and child classes when referring to inheritance. When you construct a square_ext object, there is only one object containing both the extended and base class definitions. Parent and child implies that there are two separate objects.
In your top module, you have the s_ext class variable which can invoke the extended class method s_ext.sq(). Why can’t you use that variable instead of s.sq()?
In reply to dave_59:
Hi Dave,
I couldn’t understand if I can call an overridden non virtual task from a task that was not override.
The object task that are not overridden don’t call the overridden tasks if they are not virtual.
I am having problem with the example below:
virtual class A;
task disp ();
$display(" This is class A ");
endtask
virtual task call_disp();
disp();
endtask
endclass
class EA extends A;
task disp();
$display(" This is Extended class A ");
endtask
endclass
program main;
EA my_ea;
initial
begin
my_ea = new();
my_ea.disp();
my_ea.call_disp();
end
endprogram
The result is the following:
This is Extended class A
This is class A
In reply to victor:
Can you please explain the result you were expecting and why you think it should be different than the correct results shown. Then I might be able to explain why your assumptions may not be valid.
In reply to dave_59:
Hi Dave,
I have a similar problem. In victor’s example I would expect that ‘my_ea.call_disp()’ will call EA::disp() and not A::disp(), since it is being called from an ‘EA’.
Can you explain this?
Thanks!