[fpc-pascal] Quick Modern Object Pascal Introduction, for Programmers
Michalis Kamburelis
michalis.kambi at gmail.com
Tue Jun 21 11:55:35 CEST 2016
> |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.|
The AssignTo is only used by TPersistent.Assign, so it's a "fallback"
--- if class A cannot copy *from* B, then AssignTo is called on B to
let B assign *to* A. This allows to implement the copying on any end,
which is useful e.g. when you cannot change the source code of A.
This is documented on
http://www.freepascal.org/docs-html/rtl/classes/tpersistent.assign.html
.
The short advice is that you usually want to override Assign, and
forget about AssignTo. Overriding Assign is always enough if you think
about a typical cloning scenario, when you usually copy instance of
class A to another instance of class A, and not mix classes.
Regards,
Michalis
More information about the fpc-pascal
mailing list