是什么原因导致"W1010方法'%s'隐藏基本类型'%s'的虚拟方法"?警告? [英] What causes "W1010 Method '%s' hides virtual method of base type '%s'" warning?

查看:190
本文介绍了是什么原因导致"W1010方法'%s'隐藏基本类型'%s'的虚拟方法"?警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有虚函数的基类:

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;

后代类实现具有相同名称的函数. 该函数添加了一个新参数,并调用了其anchestor函数.

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;

在编译时,显示以下警告消息:W1010方法

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

'MyFunction'隐藏基本类型'TMyBaseClass'的虚拟方法

'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?

推荐答案

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.

如果两个方法都标有overload指令,则编译器可以使用其参数列表来区分它们.没有这些,编译器所能做的就是隐藏基本方法.

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.

这篇关于是什么原因导致"W1010方法'%s'隐藏基本类型'%s'的虚拟方法"?警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆