Is there a way to create the required number of objects in Delphi without repeating?

I think C ++ supports something in the lines:

Object objects[100]; 

This will instantiate 100 objects, right? Is it possible to do this in Delphi (in particular, 2007)? Something other:

 for i:=0 to 99 do currentObject = TObject.Create; 

or using the Allocate function with the transferred size a hundred times larger than the TObject size, since it just allocates memory, it actually does not share memory and does not give it to objects. If my assumption that the C ++ instance is instantaneous and not iterative, I apologize.

+4
source share
5 answers

What you are looking for is impossible because

  • Delphi does not support static (stacked) objects.
  • Delphi objects do not have default constructors that can be automatically called by the compiler.

So this is not the lack of sugar syntax.


For full disclosure:

  • Delphi also supports the legacy "old object model" (Turbo Pascal object model), which allows statically allocated objects;
  • The distribution of dynamic objects does not interfere with the automatic syntax for creating an object, but makes such syntax undesirable;
  • The syntax for automatically creating an object is not possible because Delphi does not have default constructors: the Delphi compiler never creates objects implicitly because it does not know which constructor to call.
+5
source

As long as you cannot do what you want to use objects, if your objects are relatively simple, you can get what you want using an array of records.

Entries in Delphi can have properties (including setters and getters), as well as class and instance methods. They are created automatically upon declaration, so declaring an array of them will create them all without iteration.

For more information: http://docwiki.embarcadero.com/RADStudio/XE3/en/Structured_Types#Records_.28advanced.29 .

(I'm not sure when the new functionality was added to Delphi, it is possible that after version 2007).

+5
source

I don't know of any non-hacker way to do this other than iteration:

 var MyObjects: array[0..99] of TMyObject; i: Integer; begin for i := 0 to 99 do MyObjects[i] := TMyObject.Create; end; 
+2
source

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.

+1
source

Several are possible in Delphi (but this is not very practical in a Delphi environment unless you are writing a custom memory manager). Creating an instance of an object is a two-stage process - allocating memory for an object and building the elements of the object inside this memory. There is nothing that would require the allocation of a separate separate memory object. You can allocate a larger block of memory and build several objects inside this block using the Delphi function, which calls the constructor as a regular method if it is called from an instance variable instead of a class type, for example:

 var objects: array of Byte; obj: TSomeClass; begin SetLength(objects, 100 * TSomeClass.InstanceSize); FillChar(objects[0], 0, Length(objects)); for i := 0 to 99 do begin obj := TSomeClass.InitInstance(@objects[i * TSomeClass.InstanceSize])); obj.Create; end; ... for i := 0 to 99 do begin obj := TSomeClass(@objects[i * TSomeClass.InstanceSize]); obj.CleanupInstance; end; SetLength(objects, 0); end; 

This is not much different from what C ++ does behind the scenes when declaring an array of object instances, only C ++ supports statically declaring an array, and it will automatically call constructors and destructors, while Delphi does not support this.

There are a number of third-party implementations that allow you to allocate objects on the stack or in user buffers, for example:

Objects on the stack: Delphi Relic

Select objects on the stack at the specified memory address or through any memory manager

Just to name a couple.

0
source

Source: https://habr.com/ru/post/1442310/


All Articles