I am doing a program that solves a system of Congruences (Algebra) and I have created a class called TCongruence
with constructor, functions, class functions and so on. I have declared a private variable:
private
x: array of TCongruence;
When I try to fill this array I am writing this code:
counter := counter + 1;
SetLength(x, counter);
x[counter-1] := TCongruence.Create(...);
I have understood that this code works after some time I have spent on these 3 lines because my original code was something like this:
counter := counter + 1;
SetLength(x, counter);
tmp := TCongruence.Create(...);
x[counter-1] := tmp;
Of course I have tmp: TCongruence
. Why is the second block of code wrong?
Class are references so I thought that I could do something like that given the fact that I am not calling Free
on tmp.
Wrong = at compile time it's good but at runtime when I access the array I have weird values.