What is the difference between constructor and procedure in Delphi records?

Is there a difference in behavior between a constructor call and a procedure call in a Delphi record? I have a sample D2010 code that I want to convert to D2009 (which I use). The example uses a parameterless constructor that is not allowed in Delphi 2009. If I replace a simple, problem-free procedure call, is there any functional difference for the records?

those.

TVector = record private FImpl: IVector; public constructor Create; // not allowed in D2009 end; 

becomes

  TVector = record private FImpl: IVector; public procedure Create; // so change to procedure end; 

As far as I can see, this should work, but I can skip something.

+4
source share
3 answers

Write constructors are completely meaningless misleading syntactic sugar in Win32 native code. The only difference between the record constructor and the procedure is the syntax:

 TVector = record constructor Create; end; var vec : TVector; begin vec:= TVector.Create; 

and

 TVector = record procedure Create; end; var vec : TVector; begin vec.Create; 

AFAIK there is a difference in .NET code (I do not use .NET)

+2
source

One of the minor issues is that the record constructor should apparently be handled a bit special with respect to the normal method, since the records by default have a constructor without parameters without parameters, which should be overridden by your user version.

Another obvious difference between write constructors and write procedures is that your constructors must have at least one parameter. (Since records do not allow inheritance, and the default constructor has no parameters.)

+1
source

To build a record, a constructor is used. It is called when a record is first created.

A function can and should be called as needed.

You can create a “build” procedure, but you must name it yourself.

 TVector = record private FImpl: IVector; public procedure Create; end; var vec : TVector; begin vec.Create; 

An alternative is to create a factory function that returns an initialized record.

 function CreateVector(): TVector; begin Result.Create; end; 
0
source

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


All Articles