This ad will not create 100 objects, it will just give you an array of 100 links to objects that do not indicate anything useful.
Creating an object is a two-step process. The first step is to allocate memory (which also does not have its own code), the second step calls the constructor (Create method) to initialize this memory, create additional objects, etc. Etc.
Part of the selection can be done without a loop, but the constructor must be called to initialize each instance.
Many VCL classes do not have an additional constructor. They just have an empty constructor that does nothing. In this case, there is no need to call him.
For example, to get an array of string lists, you can use the following code configured in this example :
type TStringListArray = array of TStringList;v var Instances: array of Byte; function GetStringLists(Number: Integer): TStringListArray; var i: Integer; begin // Allocate instance memory for all of them SetLength(Instances, Number * TStringList.InstanceSize); // Zero the memory. FillChar(Instances[0], Length(Instances), 0); // Allocate array for object references. SetLength(Result, Number); for i := 0 to High(Result) do begin // Store object reference. Result[i] := @Instances[i * TStringList.InstanceSize]; // Set the right class. PPointer(Result[i])^ := TStringList; // Call the constructor. Result[i].Create; end; end;
And to get an array of 100 string lists:
var x: TStringListArray; begin x := GetStringLists(100);
Thus, although this procedure can save you some time for neglect and can theoretically be more efficient in terms of memory (less fragmentation), you still need a loop. There is no easy way out.
source share