Recent

Author Topic: Overriding nested type methods  (Read 359 times)

Sarutobi

  • Newbie
  • Posts: 1
Overriding nested type methods
« on: December 11, 2019, 03:19:52 pm »
Hello there,

is the following possible in fpc?

Code: Pascal  [Select]
  1. type ClassA = class
  2.   public
  3.     type
  4.       ClassB = class
  5.       public
  6.         procedure methodB();virtual;
  7.     end;
  8.     procedure methodA(); virtual;
  9. end;
  10.  

in an other unit or programm i want to override the methodB without redifining the inner class ClassB.
something like this:

Code: Pascal  [Select]
  1. type ClassC = class (ClassA)
  2.  public
  3.   procedure methodA();override;
  4.   procedure ClassB.methodB(); override;
  5.  

Is there a way to do this?

best regards

Cyrax

  • Hero Member
  • *****
  • Posts: 773
Re: Overriding nested type methods
« Reply #1 on: December 11, 2019, 03:24:08 pm »
No.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5897
    • wiki
Re: Overriding nested type methods
« Reply #2 on: December 11, 2019, 07:06:46 pm »
You need a new inner class.

You can do
Code: Pascal  [Select]
  1.   ClassA = class
  2.   public
  3.     type
  4.       ClassB = class
  5.       public
  6.         procedure methodB();virtual;
  7.       end;
  8.  
  9.     procedure methodA(); virtual;
  10.   end;
  11.  

 and then
Code: Pascal  [Select]
  1.   ClassChildA = class(ClassA)
  2.   public
  3.     type
  4.     ClassChildB = class(ClassB)
  5.       public
  6.         procedure methodB();override;
  7.       end;
  8.  
  9.     procedure methodA(); override;
  10.   end;
  11.  


Or, since the type is public, you can use it outside the class too
Code: Pascal  [Select]
  1.   ClassNoneNestedChildB = class(ClassA.ClassB)
  2.   public
  3.     procedure methodB();override;
  4.   end;
  5.  

jamie

  • Hero Member
  • *****
  • Posts: 2257
Re: Overriding nested type methods
« Reply #3 on: December 11, 2019, 10:53:14 pm »
And at least don't forget to call the constructors on these classes  >:(
Number 1 at blue screen app creations!