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

荒凉一梦 提交于 2019-12-10 15:38:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39095201/what-causes-w1010-method-s-hides-virtual-method-of-base-type-s-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!