Jump to content
Kryvich

Warning W1010: Method hides virtual method of base type

Recommended Posts

I like to write code without warnings and hints issued by the compiler. Today I encountered the following warning:

program TestCaseW1010;
{$APPTYPE CONSOLE}
type
  TPerson = class
    Name: string;
    function ToString(Quote: boolean): string; overload;
  end;

function TPerson.ToString(Quote: boolean): string;
begin
  if Quote then
    Result := '"' + Name + '"'
  else
    Result := Name
end;

var
  Person: TPerson;

begin
  Person := TPerson.Create;
  Person.Name := 'John';
  Writeln(Person.ToString);         // --> TPerson
  Writeln(Person.ToString(False));  // --> John
  Writeln(Person.ToString(True));   // --> "John"
  Readln;
  Person.Free;
end.

The compiler shows W1010 Method 'ToString' hides virtual method of base type 'TObject'. In fact, he does not. Is it a false positive warning, or is it something wrong with my code?

Share this post


Link to post

The correct declaration would be:

type
  TPerson = class
    Name: string;
    function ToString(Quote: boolean): string; reintroduce; overload;
  end;

 

  • Like 2
  • Thanks 1

Share this post


Link to post

Ouch! Thank you. :) 

P.S. I encountered this situation in GOLD Parser Engine 5 that I've converted from Lazarus. Now this project compiles without any warnings, I'm happy!

Edited by Kryvich
  • Like 1

Share this post


Link to post

That fact that you have to mark overloads to a virtual method as reintroduce always bothers me but I never cba to actually report it.

Share this post


Link to post

I think the requirement to explicitly state reintroduce is a good thing. It avoids the issue of forgetting to add the override directive - if you really intended to do so. 

Share this post


Link to post

From a pure linguistic point of view: Where does the need for reintroducing a purely virtual aka non existent thing come from? There seems to be a need for this that is deep in the compiler. Would be a nice research project, or something the experts can explain.

Share this post


Link to post
43 minutes ago, David Heffernan said:

Why don't you override the method from TObject? 

That's exactly the problem and as well the reason for the warning. The OP does not  actually override TObject.ToString but introduces a new method (because of the different signature). The override just does nothing. 

Edited by Zacherl

Share this post


Link to post
22 hours ago, Zacherl said:

That's exactly the problem and as well the reason for the warning. The OP does not  actually override TObject.ToString but introduces a new method (because of the different signature). The override just does nothing. 

It's not that the override does nothing. There is no override. It makes no sense to me to reintroduce a method of that name. 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×