Can I create an object of the same type as itself?
delphi
Solution
but I wonder if there will be long time/hidden negative effects.
I don't see any, and I used to do this with my own linked lists, and never had any problem. I think it is pretty much the same as creating an instance in any other place.
Problem
I have a class of mine, lets call it TMyObject, which should return a slightly modified copy of itself. So, one of its functions should return an object of the same type as itself: ``` function TMyObject.TrimEnds: TMyObject; begin Result:= TMyObject.Create; Result.DoStuff; edn; ``` Can I do that? Is it legit what am I doing? I mean, I already tried it and the compiler allows me to do it, but I wonder if there will be long time/hidden negative effects. Any thoughts will be appreciated. Thanks. Edit: The new slightly modified copy will be saved to disk. It is some kind of 'Save as...'. How it works: The original object creates a copy of itself, instructs this copy to do some changes and to save to disk. Then the original frees the copy. This way I keep the original object in memory unchanged but I have a modified version of this to disk. You may think that my object holds a picture. What I need is a function that returns a slightly modified copy of the picture.