Recent

Author Topic: New method with existing name for a descendant class  (Read 479 times)

jollytall

  • Sr. Member
  • ****
  • Posts: 320
New method with existing name for a descendant class
« on: January 08, 2024, 03:33:49 pm »
I have a simple program (demo) that does not compile:
Code: Pascal  [Select][+][-]
  1. type
  2.   tClass1 = class
  3.     procedure Foo(a : string); virtual;
  4.     end;
  5.   tClass2 = class(tClass1)
  6.     procedure Foo(a: integer); virtual;
  7.     end;
  8. procedure tClass1.Foo(a : string);
  9.   begin
  10.   writeln('Class1 - string');
  11.   end;
  12. procedure tClass2.Foo(a : integer);
  13.   begin
  14.   writeln('Class2 - integer');
  15.   end;
  16. var
  17.   MyClass : tClass2;
  18. begin
  19. MyClass := tClass2.Create;
  20. MyClass.Foo(1);
  21. MyClass.Foo('abc'); // <----- ERROR
  22. end.
However if I redefine the method with the old parameter I call it through inherited then it works:
Code: Pascal  [Select][+][-]
  1. type
  2.   tClass1 = class
  3.     procedure Foo(a : string); virtual;
  4.     end;
  5.   tClass2 = class(tClass1)
  6.     procedure Foo(a: integer); virtual;
  7.     procedure Foo(a: string); override;
  8.     end;
  9. procedure tClass1.Foo(a : string);
  10.   begin
  11.   writeln('Class1 - string');
  12.   end;
  13. procedure tClass2.Foo(a : integer);
  14.   begin
  15.   writeln('Class2 - integer');
  16.   end;
  17. procedure tClass2.Foo(a : string);
  18.   begin
  19.   inherited Foo(a);
  20.   end;
  21. var
  22.   MyClass : tClass2;
  23. begin
  24. MyClass := tClass2.Create;
  25. MyClass.Foo(1);
  26. MyClass.Foo('abc');
  27. end.
Is it by design, or it is a bug?

Also, if I use a specialized class, then I cannot use override, I have to use virtual although in the generic class the method with virtual exists.
Code: Pascal  [Select][+][-]
  1. type
  2.   tXList = class(specialize tObjectList<tX>)
  3.     function IndexOf(aX : tX) : sizeint; override; // <--- ERROR, Must be virtual
  4.     end;
Could you also help me understand, why is this?

EDIT: The second part I answered. I just missed the constref. So, it can also be "override".
Code: Pascal  [Select][+][-]
  1. type
  2.   tXList = class(specialize tObjectList<tX>)
  3.     function IndexOf(constref aX : tX) : sizeint; override; // OK
  4.     end;
« Last Edit: January 08, 2024, 03:39:43 pm by jollytall »

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: New method with existing name for a descendant class
« Reply #1 on: January 08, 2024, 06:48:46 pm »
I think you are looking for overloaded methods:

Code: Pascal  [Select][+][-]
  1.   tClass1 = class
  2.     procedure Foo(a : string); virtual; overload;
  3.     end;
  4.   tClass2 = class(tClass1)
  5.     procedure Foo(a: integer); virtual; overload;
  6.     end;
  7.  
  8.  
Be mindful and excellent with each other.
https://github.com/cpicanco/

jollytall

  • Sr. Member
  • ****
  • Posts: 320
Re: New method with existing name for a descendant class
« Reply #2 on: January 08, 2024, 09:19:16 pm »
Thank you. Indeed, that was missing. For tClass1 I do not need overload, as at the time that is defined it cannot even be assumed that once somebody in a descendant class type wants to define one with the same name. It is enough to use it in the second step.

 

TinyPortal © 2005-2018