Recent

Author Topic: [solved ] Class method inheritance doesn’t allow skipping  (Read 1477 times)

Kraig

  • New Member
  • *
  • Posts: 26
[solved ] Class method inheritance doesn’t allow skipping
« on: September 27, 2023, 02:19:14 pm »
I am making a series of classes most of which have an overriding method from the virtual method in base class, however I can’t seem to skip having a method in the most recent ancestor and have to make a dummy method in the immediate ancestor to be allowed to override it.

Here is an example

Code: Pascal  [Select][+][-]
  1. A = class (tbutton)
  2. Constructor create (the owner:tcomponet); override;
  3. Strict protected
  4. Procedure test; virtual;
  5. End;
  6.  
  7. B = class (a)
  8. Constructor create (the owner:tcomponet); override;
  9. End;
  10.  
  11. C = class (b)
  12. Constructor create (the owner:tcomponet); override;
  13. Strict protected
  14. Procedure test; override; // gives error that there is no ancestor method to override,
  15. End
  16.  
  17.  
it seems like the procedure test in class a is out of scope even though it is an ancestor . Is this behavior intentional? If so why?
« Last Edit: September 29, 2023, 02:10:45 pm by Kraig »

Thaddy

  • Hero Member
  • *****
  • Posts: 16138
  • Censorship about opinions does not belong here.
Re: Class method inheritance doesn’t allow skipping
« Reply #1 on: September 28, 2023, 03:54:25 pm »
That is simply because you did not call inherited.
If I smell bad code it usually is bad code and that includes my own code.

bytebites

  • Hero Member
  • *****
  • Posts: 680
Re: Class method inheritance doesn’t allow skipping
« Reply #2 on: September 28, 2023, 07:27:27 pm »
Does not give any error with procedure test.
Code: Pascal  [Select][+][-]
  1. program foo;
  2.  
  3. {$mode objfpc}
  4. {$H+}
  5. uses
  6.   SysUtils;
  7.  
  8. type
  9.  
  10.   { A }
  11.  
  12.   A = class
  13.     constructor Create;
  14.   strict protected
  15.     procedure test; virtual;
  16.   end;
  17.  
  18.   { B }
  19.  
  20.   B = class(A)
  21.   strict protected
  22.     procedure test; override;
  23.   end;
  24.  
  25.   { C }
  26.  
  27.   C = class(B)
  28.     constructor Create;
  29.   strict protected
  30.     procedure test; override;
  31.   end;
  32.  
  33.   { A }
  34.  
  35.   constructor A.Create;
  36.   begin
  37.  
  38.   end;
  39.  
  40.   procedure A.test;
  41.   begin
  42.  
  43.   end;
  44.  
  45.   { B }
  46.  
  47.   procedure B.test;
  48.   begin
  49.     inherited test;
  50.   end;
  51.  
  52.   { C }
  53.  
  54.   constructor C.Create;
  55.   begin
  56.  
  57.   end;
  58.  
  59.   procedure C.test;
  60.   begin
  61.     inherited test;
  62.   end;
  63.  
  64.  
  65. begin
  66. end.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
Re: Class method inheritance doesn’t allow skipping
« Reply #3 on: September 28, 2023, 09:16:05 pm »
Here is an example

Please provide a complete example that can be compiled. As bytebites showed, this should work (even if the B.test method from his example is removed).

Kraig

  • New Member
  • *
  • Posts: 26
Re: Class method inheritance doesn’t allow skipping
« Reply #4 on: September 29, 2023, 03:09:15 am »
Sorry for all the trouble,  I believe that I have found the problem I don’t really understand why it happened though. I had a method in immediate ancestor with same name but Containing a parameter
I thought that it would be obvious that they didn’t match but it seems to only go by name instead of name with parameter. Adding overload seemed to solve it but I’m still not sure why it got confused.

Code: Pascal  [Select][+][-]
  1.  
  2. A = class (tbutton)
  3. Constructor create (the owner:tcomponet); override;
  4. Strict protected
  5. Procedure test; virtual;
  6. End;
  7.  
  8. B = class (a)
  9. Constructor create (the owner:tcomponet); override;
  10. Strict protected
  11. Procedure test (const x:string); overload;
  12. End;
  13.  
  14. C = class (b)
  15. Constructor create (the owner:tcomponet); override;
  16. Strict protected
  17. Procedure test; override; // was being blocked by Procedure test (const x:string);
  18. End
  19.  
  20.  

When I tried using virtual on procedure with same name instead of overload, it didn’t work. I guess this means that there can’t be different virtual procedures with the same name?
« Last Edit: September 29, 2023, 03:16:50 am by Kraig »

ASerge

  • Hero Member
  • *****
  • Posts: 2336
Re: Class method inheritance doesn’t allow skipping
« Reply #5 on: September 29, 2023, 03:30:32 am »
When I tried using virtual on procedure with same name instead of overload, it didn’t work. I guess this means that there can’t be different virtual procedures with the same name?
In this case, the reintroduce directive is used.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. type
  3.   TA = class
  4.   strict protected
  5.     procedure Test; virtual;
  6.   end;
  7.  
  8.   TB = class(TA)
  9.   strict protected
  10.     procedure Test(const X: string); overload;
  11.   end;
  12.  
  13.   TC = class(TB)
  14.   strict protected
  15.     procedure Test(X: SizeInt); reintroduce;
  16.   end;
  17.  
  18. procedure TA.Test;
  19. begin
  20. end;
  21.  
  22. procedure TB.Test(const X: string);
  23. begin
  24. end;
  25.  
  26. procedure TC.Test(X: SizeInt);
  27. begin
  28. end;
  29.  
  30. begin
  31. end.
« Last Edit: September 29, 2023, 03:32:25 am by ASerge »

Kraig

  • New Member
  • *
  • Posts: 26
Re: Class method inheritance doesn’t allow skipping
« Reply #6 on: September 29, 2023, 03:32:14 am »
Oh ? I’ve never heard of that one ..

ASerge

  • Hero Member
  • *****
  • Posts: 2336
Re: Class method inheritance doesn’t allow skipping
« Reply #7 on: September 29, 2023, 03:35:07 am »
Oh ? I’ve never heard of that one ..
In my example, C.Test can also be virtual.

Kraig

  • New Member
  • *
  • Posts: 26
Re: Class method inheritance doesn’t allow skipping
« Reply #8 on: September 29, 2023, 03:36:30 am »
I just tried reintroduce in place of the overload but it didn’t compile ..
ah the reintroduce is used after the overload  :D
I think I’ve seen reintroduce used with making new types of constructors.
« Last Edit: September 29, 2023, 04:20:38 am by Kraig »

 

TinyPortal © 2005-2018