Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes "W1010 Method '%s' hides virtual method of base type '%s'" warning?

I've a base class with a virtual function:

TMyBaseClass = class(TObject)
public
  ValueOne : integer;
  procedure MyFunction(AValueOne : integer); virtual;
end;

procedure TMyBaseClass.MyFunction(AValueOne : integer);
begin
  ValueOne := ValueOne;
end;

A descendant class implements a function with the same name. This function adds a new param and calls its anchestor's function.

TMyDerivedClass = class(TMyBaseClass)
public
  ValueTwo : integer;
  procedure MyFunction(AValueOne : integer; AValueTwo : integer);
end;

procedure TMyDerivedClass.MyFunction(AValueOne : integer; AValueTwo : integer);
begin
  inherited MyFunction(AValueOne);
  ValueTwo := ValueTwo;
end;

While compiling, the following warning message is shown: W1010 Method

'MyFunction' hides virtual method of base type 'TMyBaseClass'

I found a solution to the problem reading another question, but I'm wondering about what's causing this warning. Does TMyDerivedClass.MyFunction hides TMyBaseClass.MyFunction even if the two functions have different parameters? If so, why?

like image 565
Fabrizio Avatar asked Aug 23 '16 07:08

Fabrizio


1 Answers

The documentation explains the issue quite clearly:

You have declared a method which has the same name as a virtual method in the base class. Your new method is not a virtual method; it will hide access to the base's method of the same name.

What is meant by hiding is that from the derived class you no longer have access to the virtual method declared in the base class. You cannot refer to it since it has the same name as the method declared in the derived class. And that latter method is the one that is visible from the derived class.

If both methods were marked with the overload directive then the compiler could use their argument lists to discriminate between them. Without that all the compiler can do is hide the base method.

Read the rest of the linked documentation for suggestions on potential resolutions.

like image 71
David Heffernan Avatar answered Oct 18 '22 03:10

David Heffernan