Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The compiler does not give a warning while hiding member variables and/or procedures

When I forgot to add the key words 'virtual' and 'override' recently I would have expected a compiler warning when I accidentally used the same name for a procedure in a derived class. I got none and now I do not understand why. What do I need to do to get warnings for hiding base members and methods?

According to this answer (by Jim McKeeth, who is undoubtedly correct):

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.

However, to my surprise this code does not give me 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.

I am using Delphi XE and my compiler says all is fine:

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

My guess is that either I misunderstand the aforementioned quote by Jim McKeeth or I have some setting in my compiler that I am not aware of (I did test it on one other computer by the way, same results). Any help would be greatly appreciated.

like image 497
SpaghettiCook Avatar asked Jul 01 '13 16:07

SpaghettiCook


1 Answers

The documentation describes these particular warnings as follows:

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).)

Neither warning applies to your code. In the case of HIDDEN_VIRTUAL, you do not have any virtual methods. And in the case of HIDING_MEMBER you do not have any properties.

Follow the links in the quoted section above (or from the main documentation link in the first sentence) to find the full details for these warnings.

like image 149
David Heffernan Avatar answered Sep 30 '22 17:09

David Heffernan