Recent

Author Topic: Assign to Inheritance  (Read 3355 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Assign to Inheritance
« on: August 31, 2021, 10:05:30 am »
Hello :),

How should I overwrite the AssignTo() method multiple times ?

Example:

Super class
Code: Pascal  [Select][+][-]
  1. procedure TElement.AssignTo(Dest: TPersistent);
  2. var
  3.    NewDestElem : TElement;
  4. begin
  5.    IF Dest is TElement then begin
  6.       NewDestElem := TElement(Dest);
  7.    end;
  8.  
  9.  
  10.    //Todo
  11.    //Assign Super Class Properties
  12. end;
  13.  
Sub class
Code: Pascal  [Select][+][-]
  1. procedure TFrontWall.AssignTo(Dest: TPersistent);
  2. var
  3.    NewDestElem : TElement;
  4. begin
  5.    IF Dest is Theadwall then begin
  6.       inherited AssignTo(Dest);  //---> do i have to Copy Procedure of SuperClass or can i just call the Method ?
  7.  
  8.       NewDestElem := TFrontWall(Dest);
  9.    end;
  10.  
  11.    //Todo
  12.    //Assign Super Class Properties AND Sub Class properties
  13. end;
  14.  
I don't think this will work ?

Thanks For any Reply :)
« Last Edit: August 31, 2021, 11:05:20 am by Weitentaaal »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1572
    • Lebeau Software
Re: Assign to Inheritance
« Reply #1 on: August 31, 2021, 06:51:57 pm »
How should I overwrite the AssignTo() method multiple times ?

I would write it like this:

Code: Pascal  [Select][+][-]
  1. procedure TElement.AssignTo(Dest: TPersistent);
  2. var
  3.   DestElem : TElement;
  4. begin
  5.   if Dest is TElement then begin
  6.     DestElem := TElement(Dest);
  7.     // assign only TElement properties to DestElem...
  8.   end
  9.   else begin
  10.     inherited AssignTo(Dest); // raise runtime error
  11.   end;
  12. end;
  13.  
  14. procedure TFrontWall.AssignTo(Dest: TPersistent);
  15. var
  16.   DestWall : TFrontWall;
  17. begin
  18.   inherited AssignTo(Dest); // copy TElement properties first, or raise error
  19.   if Dest is TFrontWall then begin
  20.     DestWall := TFrontWall(Dest);
  21.     // assign only TFrontWall properties to DestWall...
  22.   end;
  23. end;
  24.  
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: Assign to Inheritance
« Reply #2 on: September 01, 2021, 09:43:18 am »
Is it necessary to call the overwritten method of the superclass again ?

Example:
Code: Pascal  [Select][+][-]
  1. constructor TSuperClass.Create();
  2. begin
  3.    inherited Create;
  4.  
  5.    //Init TSuperClass Variables
  6. end;
  7.  
  8. constructor TSubClass.Create();
  9. begin
  10.    inherited Create; //--> Do i have to call Superclass Create ? or just Assign the Superclass Properties here ?
  11.  
  12.    //Init TSubClass Variables
  13. end;
  14.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1572
    • Lebeau Software
Re: Assign to Inheritance
« Reply #3 on: September 01, 2021, 06:42:45 pm »
Is it necessary to call the overwritten method of the superclass again ?

Construction and Assignment are two different operations.  A constructor should always call the inherited constructor to let its base class initialize itself.  You should not use Assign/To() inside a constructor.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018