Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting C++ struct to Delphi

Tags:

c++

struct

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.

like image 941
oopbase Avatar asked Dec 17 '22 17:12

oopbase


1 Answers

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;
like image 120
Rob Kennedy Avatar answered Dec 19 '22 07:12

Rob Kennedy