[fpc-pascal] Quick Modern Object Pascal Introduction, for Programmers
Marco van de Voort
marcov at stack.nl
Tue Jun 21 12:14:14 CEST 2016
In our previous episode, Michalis Kamburelis said:
>
> - A section about TPersistent.Assign. This is the "basic approach to
> cloning" that should probably be shown first. See it here:
> http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_cloning_tpersistent_assign
See also
http://stackoverflow.com/questions/4041760/correct-way-to-duplicate-delphi-object/4041906#4041906
That is for an own root class and pre class helpers, but you could do it also in or TPersistent
helper:
type tpersisthelp = class helper for TPersistent
function Clone(t: TPersistentClass = nil): TPersistent;
end;
function tpersisthelp.Clone(t: TPersistentClass = nil): TPersistent;
begin
if Assigned(t) then
Result := t.Create
else
Result := TPersistentClass(Self.ClassType).Create;
Result.Assign(Self);
end;
Since TComponent declares a virtual destructor that might even be better.
More information about the fpc-pascal
mailing list