Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cloning of objects

Posted on 1997-04-30
8
Medium Priority
?
1,126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

721 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