Duplicating a class instance

Duplicating a class instance

I’ve run into a strange problem, and I wonder if there is a solution out there.

I have working code that do this

  aCopy := TMyClass(Self.ClassType.Create);

The .Create will also eventually call .AfterConstruction – which poses a problem – as I do things in it which basically shouldn’t be done with the copy.

So – I add another constructor to the base class to set a flag to indicate it is a copy  – but – how do I invoke the other constructor?  

A cast like the one below, is not a viable solution, it seems – as I end up with a nil pointer AV.

  aCopy := TMyClass(Self.ClassType).AnotherCreate;

How do I invoke the right constructor in a clean and efficient way?

7 thoughts on “Duplicating a class instance

  1. It is used in Application.CreateForm, TReader and CreateParentedControl, so it’s probably not so evil 😉

    but my favorite solution will be a method like this

    aCopy := AnotherInstance();

    function TMyClass.AnotherInstance: TMyClass;


      Result := TMyClass(NewInstance);

      Result.FAnother := True;




    function TMyClass.AnotherInstance: TMyClass;


      Result := TMyClass.AnotherCreate();


  2. If AfterConstruction does something that should not be done in all cases where you create a new instance, then you are doing something wrong. Solve that problem and all the other problems cease to exist.

  3. Stefan Glienke – I agree. It’s a kludge, but it was necessary to avoid duplicate objects used for dirty checking, being added to the instance dictionary – and that is revealing only a tiny part of the monsters hiding in our class hierarchy.

Leave a Reply