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

标签 delphi polymorphism compiler-warnings

最近,当我忘记添加关键字“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 W1010 Method '%s' hides virtual method of base type '%s' (Delphi).)

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 W1009 Redeclaration of '%s' hides a member in the base class (Delphi).)

这两个警告都不适用于您的代码。对于HIDDEN_VIRTUAL,您没有任何虚拟方法。对于 HIDING_MEMBER,您没有任何属性。

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

关于delphi - 隐藏成员变量和/或过程时编译器不会发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17409057/

相关文章:

delphi - GridPanel 在第一次调整大小时不调整

TextMate 的 Delphi 语法

java - 如何在 if 条件下重构一个复杂的表达式?

java - 为警告创建 Java 注释 - @NonOptimal

haskell - 在 GHC 中禁用 "Module does not export identifier"警告

delphi - delphi中如何设置窗体的宽度和高度

delphi - 监视目录中的文件更改 - 在 JCL 中实现?

C# 多态属性

C++ 多态性和 new 关键字

c - 赋值 <指向常量数组的指针> = <指向数组的指针> : incompatible pointers