Solved

Cloning of objects

Posted on 1997-04-30
8
1,095 Views
Last Modified: 2013-11-23
How do I give an object a method, so that it can clone itself to a new object of the same type. The objects will have the same ancestor but I will not know precise type at design time.

See my last comment for an answer
Of cource it can be done by testing IF (Original is TypeA) then Clone := TypeA.Create else IF (original is TypeB....

but it must can be done in a better way.              
0
Comment
Question by:jph
8 Comments
 
LVL 1

Expert Comment

by:Heapster
ID: 1335814
WHat about writing a .Clone method for all your objects, and creating a copy of the object in there, then changing the parent property to whatever is appropriate.
0
 

Author Comment

by:jph
ID: 1335815
For 100 points, I would like to see an small example with objects OriginalA and OriginalB inherited fra TOriginalA and a Cloned, that will be of type TOriginalA if Cloned := OriginalA.Clone and of type TOriginalB if Cloned := OriginalB.Clone;
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335816
Surely each descendant can have its own clone method. The original has a clone method declared as virtual. That way, whatever type the object is, it will call its own clone method....

Is it really this simple or have I missed something here?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:jph
ID: 1335817
Edited text of question
0
 

Author Comment

by:jph
ID: 1335818
This was the kind of answer I was looking for:

unit CloneUnit;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, EnhButtn;
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

TFoo = class
  Name : string;
  constructor Create; virtual;
  constructor Clone(const aFoo: TFoo);
  function CloneMyself: TFoo; virtual;
  procedure WhoIAm; virtual;
end; {TFoo}


THoo = class(TFoo)
  constructor Create; override;
  constructor Clone(const aHoo:THoo);
  function CloneMyself: TFoo; override; // note return type
  procedure WhoIAm; override;
end; {TGoo}

var
  Form1: TForm1;

implementation

{$R *.DFM}

constructor TFoo.Create;
begin
  Name := 'Testing Foo';
end;

constructor TFoo.Clone(const aFoo: TFoo);
begin
  name  := aFoo.name;
end;

function TFoo.CloneMyself: TFoo;      {virtual}
begin
      Result := Clone(self);
end;      {CloneMyself}

procedure TFoo.WhoIAm;      {override}
begin
   ShowMessage('I am a TFoo ');
end;      {WhoIAm}

 
constructor THoo.Create;
begin
  Name := 'Testing Hoo';
end;

constructor THoo.Clone(const aHoo: THoo);
begin
  name  := aHoo.name;
end;

function THoo.CloneMyself : TFoo;      {virtual}
begin
      Result := Clone(self);
end;      {CloneMyself}


procedure THoo.WhoIAm;      {override}
begin
   ShowMessage('I am a THoo ');
end;      {WhoIAm}


procedure TForm1.Button2Click(Sender: TObject);
var
  a, b: TFoo;
  s: string;
begin
  a := THoo.Create;      // polymorphic creation
  b := a.CloneMyself;      // b doesn't know what it'll be
  b.WhoIAm;
end;

end.

0
 

Expert Comment

by:cucumber
ID: 1335819
Clone is too "java"ish. Have a look at TPersistent.Assign.  That's the delphi way to do it.
0
 
LVL 1

Expert Comment

by:sleach
ID: 1335820
I found this example in the Delphi Help files.  I think it might be an easy way to do what you want...

Example

type
  {A variable of type TComponentRef can be set at run time to refer to TComponentClass or any of its subclasses}
  TComponentRef = class of TComponentClass;

var
  TCRef: TComponentRef;
  NewComponent: TComponent;
begin
TCRef := TButton; {TCRef can now be used anywhere the name of the class TButton could be used}
NewComponent := TCRef.Create; {TCRef is used here to create a new TButton}
end;

0
 

Accepted Solution

by:
batsoft earned 100 total points
ID: 1335821
{First we need declare virtual constructor at
 ffor base class TFoo}
class
 TFoo = class
 private
 FirstField:Integer;
  public
    Constructor CreateFoo;virtual;
....
end;
 THoo =  class(TFoo)
  public
    Constructor CreateFoo;override;
 .......
end;
{in descendants we will override constrcutor and use this}

function Clone(Src:TFoo):TFoo; {can be hoo from foo or any from Foo}
begin
  Result := (Src.Class.ClassType as TFoo).CreateFoo;
  Move(Result.FirstField,Src.FirstField,SizeOF(Src));
end;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question