[fpc-pascal] Quick Modern Object Pascal Introduction, for Programmers
Dennis Poon
dennis at avidsoft.com.hk
Tue Jun 21 10:30:04 CEST 2016
>> 2) use of ClassType function for cloning objects
> I added two things:
>
> - 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
> . I mentioned the special rules when calling the "inherited" from
> Assign overrides, and AssignTo, and the fact that TPersistent makes
> the default visibility "published".
>
> - And the ClassType, with the example of Clone method using it, is
> added at the desciption of "class references" on
> http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_class_references
>
Just read your example
|function TMyClass.Clone(AOwner: TComponent): TMyClass; begin // This
would always create an instance of exactly TMyClass: //Result :=
TMyClass.Create(AOwner); // This can potentially create an instance of
TMyClass descendant: Result := TMyClassRef(ClassType).Create(AOwner);
Result.Assign(Self); end |
|I remember someone once asked whether we should override the method
Assign or AssignTo.|
|I am worried, choosing the wrong method to override will produce
unexpected result.|
||
|Dennis|
||
|
|
More information about the fpc-pascal
mailing list