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.