Clone a instance of object

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Michael Van Canneyt


On Mon, 23 Sep 2013, Frederic Da Vitoria wrote:

>
> Yes. But that works only for 'simple' types and still requires you to write code.
>
> For the more general case where you can have arrays, classes, interfaces and whatnot as fields of your object, there is simply no correct way.
>
>
> Isn't there? Maybe it wouldn't be easy, but IIRC Delphi offers something related to this with TComponents and Streams. This Delphi mechanism I knew worked only for properties,
> but I found references that there existed a wider mechanism in D2010.

There is no GENERAL way.

The RTTI based mechanisms make special assumptions about how to reference components, tpersistents and whatnot.

If I have a field that is a stream. How are you going to correctly copy this ?
Just the stream instance pointer ? The stream+Contents ?

The answer depends on how the classes are supposed to behave. The language itself cannot give you this mechanism.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

zaher dirkey
In reply to this post by Graeme Geldenhuys-3

On Mon, Sep 23, 2013 at 12:58 PM, Graeme Geldenhuys <[hidden email]> wrote:

Also be careful about the pitfalls of cloning a class. eg: How you want
to handle more complex cases. eg: Cloning a class with a reference to
another class, or cloning a class with an embedded class (field), etc...
tiOPF's implementation allows the developer to decided how such cases
should be handled.

​The idea is simply than needs to use a stream or RTTI, it is like a record, if looked at an object like as a record but it is inheritable.
​Compile can now every object body, and do the assigning as record assign.
That's the feature I like to have. ​

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

zaher dirkey

On Mon, Sep 23, 2013 at 4:28 PM, Zaher Dirkey <[hidden email]> wrote:
​The idea is simply than needs to use a stream or RTTI, it is like a record, if looked at an object like as a record but it is inheritable.
​Compile can now every object body, and do the assigning as record assign.
That's the feature I like to have. ​

Best Regards

​This is a funny example about what in my mind about what I talk (I know it is stupid and unsafe)
If you look it is only for the class that not yet inherited yet, and work only for the top class, but the compiler can guss all inheited/derived classes and make the same assign.
If some understand it, maybe give more explain.
​-----------------------------------------------------
type
  TmyRec = record
    s: string;
    i: integer;
  end;
  PmyRec=^TmyRec;

  TmyObj = class(TObject)
    s: string;
    i: integer;
  end;


procedure TForm1.Button1Click(Sender: TObject);
var
  o1, o2: TmyObj;
begin
  o1 := TmyObj.Create;
  o2 := TmyObj.Create;
  o1.s := 'test1';
  o1.i := 122;
  PmyRec(@o2)^ := PmyRec(@o1)^;
  WriteLn(o2.s);
  WriteLn(o2.i);
end;
​-----------------------------------------------------​

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12