What causes the "W1010 Method"% s "to hide the virtual method of the base type"% s "? Warning?

I have 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; 

The descendant class implements a function with the same name. This function adds a new parameter and calls its orchestra 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; 

When compiling, the following warning message is displayed: W1010 Method

"MyFunction" hides the virtual method of the base type "TMyBaseClass"

I found a solution to the problem by reading another question , but I wonder what causes this warning. Does TMyDerivedClass.MyFunction support TMyBaseClass.MyFunction, even if two functions have different parameters? If so, why?

+5
inheritance delphi overrides
source share
1 answer

The documentation explains the problem quite clearly:

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

What is hiding is that from the derived class you no longer have access to the virtual method declared in the base class. You cannot reference it because it has the same name as the method declared in the derived class. And this last method is the one that is visible from the derived class.

If both methods were marked with the overload directive, then the compiler can use its argument lists to distinguish between them. Without this, all the compiler can do is hide the underlying method.

Read the rest of the related documentation for suggestions on possible permissions.

+8
source share

All Articles