微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

delphi – 编译器在隐藏成员变量和/或过程时不会发出警告

当我忘记最近添加关键词’virtual’和’override’时,我会在意外地为派生类中的过程使用相同的名称时发出编译器警告.我没有,现在我不明白为什么.我需要做些什么来获取隐藏基本成员和方法的警告?

根据this answer(Jim McKeeth,毫无疑问是正确的):

If you declare a method in a descendant class that has the same name as a method in an ancestor class then you are hiding that ancestor method – meaning if you have an instance of that descendant class (that is referenced as that class) then you will not get the behavior of the ancestor. The compiler will give you a warning.

但是,令我惊讶的是这段代码没有给我一个警告:

unit Unit1;

interface

{$WARNINGS ON}
{$WARN HIDING_MEMBER ON}
{$WARN HIDDEN_VIRTUAL ON}
// I understand the two lines above are superfluous.
// I put them there to demonstrate that I have tried to enable these
// warnings explicitly.

type
    TBase = class
    public
        SomeMember: integer;
        procedure Foo;
    end;

type
    TDerived = class (TBase)
    public
        SomeMember: integer;
        procedure Foo;
    end;

implementation


{ TBase }

procedure TBase.Foo;
begin

end;

{ TDerived }

procedure TDerived.Foo;
begin

end;

end.

我正在使用Delphi XE,我的编译器说一切都很好:

Checking project dependencies…
Building Project1.dproj (Debug,Win32)
dcc command line for “Project1.dpr”
c:\program files\embarcadero\rad studio\8.0\bin\dcc32.exe -$O- -$W+ -$YD –no-config -B -Q -AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;
DbiProcs=BDE;DbiErrs=BDE -DDEBUG -E”C:\Compiler Output” -I”c:\program files\embarcadero\rad studio\8.0\lib\Win32\debug”;”c:\program
files\embarcadero\rad studio\8.0\RaveReports\Lib”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\debug”;”c:\program files\embarcadero\rad
studio\8.0\Imports”;”C:\Users\Public\Documents\RAD Studio\8.0\Dcp”;”c:\program files\embarcadero\rad studio\8.0\include”;”C:\Program
Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\release”;”c:\program files\embarcadero\rad
studio\8.0\RaveReports\Lib” -LE”C:\Users\Public\Documents\RAD Studio\8.0\Bpl” -LN”c:\program files\embarcadero\rad studio\8.0\bin\Dcp”
-N0″C:\Compiler Output\DCU” -O”c:\program files\embarcadero\rad studio\8.0\Imports”;”C:\Users\Public\Documents\RAD Studio\8.0\Dcp”;”c:\program
files\embarcadero\rad studio\8.0\include”;”C:\Program Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\release”;
“c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib” -R”c:\program files\embarcadero\rad studio\8.0\Imports”;”C:\Users\Public\Documents\RAD
Studio\8.0\Dcp”;”c:\program files\embarcadero\rad studio\8.0\include”;”C:\Program Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad
studio\8.0\lib\win32\release”;”c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib” -U”c:\program files\embarcadero\rad
studio\8.0\lib\Win32\debug”;”c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib”;”c:\program files\embarcadero\rad
studio\8.0\lib\win32\debug”;”c:\program files\embarcadero\rad studio\8.0\Imports”;”C:\Users\Public\Documents\RAD Studio\8.0\Dcp”;”c:\program
files\embarcadero\rad studio\8.0\include”;”C:\Program Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\release”;
“c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib” -K00400000 -NB”c:\program files\embarcadero\rad studio\8.0\bin\Dcp”
-NH”C:\Users\Public\Documents\RAD Studio\8.0\hpp” -NO”C:\Compiler Output\DCU” Project1.dpr
Success
Elapsed time: 00:00:00.2

我的猜测是,我误解了Jim McKeeth的上述引用,或者我在编译器中有一些我不知道的设置(顺便提一下,我在另一台计算机上测试了它,结果相同).任何帮助将不胜感激.

解决方法

documentation描述了这些特殊警告如下:

HIDDEN_VIRTUAL: Turns on or off warnings produced when a descendant declares a method of the same name as a method in an
ancestor,and the ancestor method is virtual,but the descendant’s
method is not an override.
(See 07001.)

HIDING_MEMBER: Turns on or off warnings produced when a descendant declares a new property of the same name as a property in an ancestor.
(See 07002.)

这两个警告都不适用于您的代码.在HIDDEN_VIRTUAL的情况下,您没有任何虚拟方法.在HIDING_MEMBER的情况下,您没有任何属性.

按照上面引用部分(或第一句中的主文档链接)中的链接查找这些警告的完整详细信息.

原文地址:https://www.jb51.cc/delphi/101820.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐