Forum > Beginners

[solved ] Class method inheritance doesn’t allow skipping

(1/2) > >>

Kraig:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---A = class (tbutton)Constructor create (the owner:tcomponet); override;Strict protectedProcedure test; virtual;End; B = class (a)Constructor create (the owner:tcomponet); override;End; C = class (b) Constructor create (the owner:tcomponet); override;Strict protectedProcedure test; override; // gives error that there is no ancestor method to override,End   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?

Thaddy:
That is simply because you did not call inherited.

bytebites:
Does not give any error with procedure test.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program foo; {$mode objfpc}{$H+}uses  SysUtils; type   { A }   A = class    constructor Create;  strict protected    procedure test; virtual;  end;   { B }   B = class(A)  strict protected    procedure test; override;  end;   { C }   C = class(B)    constructor Create;  strict protected    procedure test; override;  end;   { A }   constructor A.Create;  begin   end;   procedure A.test;  begin   end;   { B }   procedure B.test;  begin    inherited test;  end;   { C }   constructor C.Create;  begin   end;   procedure C.test;  begin    inherited test;  end;  beginend.

PascalDragon:

--- Quote from: Kraig on September 27, 2023, 02:19:14 pm ---Here is an example
--- End quote ---

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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- A = class (tbutton)Constructor create (the owner:tcomponet); override;Strict protectedProcedure test; virtual;End; B = class (a)Constructor create (the owner:tcomponet); override;Strict protected Procedure test (const x:string); overload;End; C = class (b)Constructor create (the owner:tcomponet); override;Strict protectedProcedure test; override; // was being blocked by Procedure test (const x:string);End  
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?

Navigation

[0] Message Index

[#] Next page

Go to full version