In C++ there's a common idiom in assignment operators where you must avoid problems caused by copying an object to itself.
http://www.parashift.com/c++-faq/self-assignment-why.html
But, in Delphi VCL (at least in D2010) this doesn't seem to be handled well.
As an example, the following function will clear the TStringList
passed to it!
procedure foo(var strings:TStringList)
begin
strings.Assign(strings);
end;
Is this still an issue with later versions of Delphi - or is there a common way of handling the problem?
Self assignment is rare in Delphi, which is why it is not commonly checked for. Since class instances must be allocated on the heap, and the assignment operator is not overridable in classes, assignment from one object to another must be done explicitly, such as via TPersistent.Assign()
. The majority of Delphi users understand that and do not write code that would assign an object to itself, accidentally or otherwise.
Self assignment is more common in C++, especially since it can actually be generated by the compiler itself in some cases. And the assignment operator is overloadable. So it makes more sense to do self assignment checks in C++.