Porting a C ++ structure in Delphi

First, let me show you the structure:

struct HPOLY { HPOLY() : m_nWorldIndex(0xFFFFFFFF), m_nPolyIndex(0xFFFFFFFF) {} HPOLY(__int32 nWorldIndex, __int32 nPolyIndex) : m_nWorldIndex(nWorldIndex), m_nPolyIndex(nPolyIndex) {} HPOLY(const HPOLY& hPoly) : m_nWorldIndex(hPoly.m_nWorldIndex), m_nPolyIndex(hPoly.m_nPolyIndex) {} HPOLY &operator=(const HPOLY &hOther) { m_nWorldIndex = hOther.m_nWorldIndex; m_nPolyIndex = hOther.m_nPolyIndex; return *this; } bool operator==(const HPOLY &hOther) const { return (m_nWorldIndex == hOther.m_nWorldIndex) && (m_nPolyIndex == hOther.m_nPolyIndex); } bool operator!=(const HPOLY &hOther) const { return (m_nWorldIndex != hOther.m_nWorldIndex) || (m_nPolyIndex != hOther.m_nPolyIndex); } __int32 m_nPolyIndex, m_nWorldIndex; }; 

There are some things that I do not understand.

What does HPOLY repetition inside a structure mean? And how to transcribe structures into delphi code?

Thank you for your help.

+4
source share
2 answers

Repeating HPOLY within a structure is the constructor definition for this type. In Delphi, the copy constructor (the third in C ++ that creates an instance of this type based on another instance of the same type) is not needed in Delphi. A constructor with two arguments allows you to specify the initial values ​​for two fields. The default constructor with a null argument sets the field values ​​to -1, but Delphi does not allow such a constructor in records.

The next section of this structure is the assignment operator. Delphi provides automatic recording of records. The following are comparison operators that compare the type of equality and inequality. The compiler will call them when you use the = and <> operators for HPoly values.

 type HPoly = record m_nPolyIndex, m_nWorldIndex: Integer; constructor Create(nWorldIndex, nPolyIndex: Integer); class operator Equal(const a: HPoly; const b: HPoly): Boolean; class operator NotEqual(const a: HPoly; const b: HPoly): Boolean; end; constructor HPoly.Create(nWorldIndex, nPolyIndex: Integer); begin m_nPolyIndex := nPolyIndex; m_nWorldIndex := nWorldIndex; end; class operator HPoly.Equal(const a, b: HPoly): Boolean; begin Result := (a.m_nPolyIndex = b.m_nPolyIndex) and (a.m_nWorldIndex = b.m_nWorldIndex); end; class operator HPoly.NotEqual(const a, b: HPoly): Boolean; begin Result := (a.m_nPolyIndex <> b.m_nPolyIndex) or (a.m_nWorldIndex <> b.m_nWorldIndex); end; 
+8
source

HPOLY is a structure with two 32-bit integer fields: m_nPolyIndex and m_nWorldIndex .

The first three lines are called contructors: code that runs whenever a new instance of HPOLY . Then, writing the variable names after the colon means initializing the contents of the variable.

For example, creating an empty HPOLY:

 HPOLY x; 

The first empty constructor is called on x. The value of x.m_nWorldIndex is 0xFFFFFFFF, and the value of x.m_nPolyIndex is 0xFFFFFFFF.

Two other constructors manually initialize the values ​​of two fields:

 XPOLY y( 1, 2 ); XPOLY z( y ); 

The value of y.m_nWorldIndex is 1, and the value of y.m_nPolyIndex is 2.

The value of z.m_nWorldIndex is y.m_nWorldIndex , and the value of z.m_nPolyIndex is y.m_nPolyIndex .

In Delphi, the TPOLY structure can be translated into the following entry:

 TPOLY = Record m_nWorldIndex : Integer; m_nPolyIndex : Integer; end; 
+4
source

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


All Articles