How to copy data from one class to another class

advertisements

how to copy data from one class into a second class using operator overloading with DELPHI ?

my dummy app goes like this :

type
  TClass_A = class
    a: String;
  end;
  TClass_B = class(TClass_A)
    b: String;
  end;

implementation    

procedure TForm1.Button1Click(Sender: TObject);
var
  a: TClass_A;
  b: TClass_B;
begin
  a := TClass_A.Create;
  b := TClass_B.create;

  b := a;  // <<-- What code should be here? Can I overload := operator?
end;


The assignment operator cannot be overloaded in Delphi.

You will need to introduce a method to perform the copying. An example of how this might be done is TPersistent.Assign. It would be perfectly reasonable for you to derive from TPersistent and override the virtual Assign method to implement the desired functionality.