Conseqences of assigning self

Posted by Vegar on Stack Overflow See other posts from Stack Overflow or by Vegar
Published on 2010-04-06T12:46:26Z Indexed on 2010/04/06 13:33 UTC
Read the original article Hit count: 275

Filed under:
|
|

Hi,

Found a piece of code today, that I find a little smelly...

TMyObject.LoadFromFile(const filename: String);
begin
  if fileExists(filename) then
    self := TSomeObjectStreamer.ReadObjectFromFile(filename);
end;

If this code works, it will atleast leak some memory, but does it work?
Is OK to assign to self in this manner?

What if the streamed object is of a different subclass then the original self?
What if the streamed object is of a different class with no common ancestore to the original self?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about self