Solved

Cloning of objects

Posted on 1997-04-30
8
1,073 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
 

Author Comment

by:jph
ID: 1335817
Edited text of question
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now