oopbase
oopbase

Reputation: 11385

Porting C++ struct to Delphi

First, let me show you the struct:

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 I don't understand.

What does the repetition of HPOLY inside the struct mean? And how to transcript structs to delphi code?

Thank you for your help.

Upvotes: 4

Views: 762

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163247

The repetition of HPOLY inside the struct are definitions of constructors for that type. In Delphi, the copy constructor (the third one in the C++, which constructs an instance of this type based on another instance of the same type) not necessary in Delphi. The two-argument constructor lets you specify initial values for the two fields. The default, zero-argument constructor sets the fields' values to -1, but Delphi doesn't allow such a constructor on records.

The next section in that struct is the assignment operator. Delphi provides that for records automatically. Next are comparison operators that compare the type for equality and inequality. The compiler will invoke them when you use the = and <> operators on 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;

Upvotes: 8

vz0
vz0

Reputation: 32923

HPOLY is an struct with only two 32 bit integer fields: m_nPolyIndex and m_nWorldIndex.

The first three lines are called contructors: code that gets executed whenever a new HPOLY instance is created. Then, writing the variable names after the colon means initializing the variable content.

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.

The other two constructors are manually initializing the two fields values:

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.

On Delphi the TPOLY struct can be translated to the following record:

TPOLY = Record
  m_nWorldIndex : Integer;
  m_nPolyIndex : Integer;
end; 

Upvotes: 4

Related Questions