首先,让我向您展示结构:
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;
};
Run Code Online (Sandbox Code Playgroud)
有一些我不明白的事情.
结构内部HPOLY的重复意味着什么?以及如何将结构转录为delphi代码?
谢谢您的帮助.
结构中HPOLY的重复是该类型的构造函数的定义.在Delphi中,复制构造函数(C++中的第三个,它基于另一个相同类型的实例构造此类型的实例)在Delphi中不是必需的.双参数构造函数允许您指定两个字段的初始值.默认的零参数构造函数将字段的值设置为-1,但Delphi不允许在记录上使用这样的构造函数.
该结构中的下一部分是赋值运算符.Delphi自动为记录提供.接下来是比较运算符,比较相等和不等式的类型.当您在值上使用=和<>运算符时,编译器将调用它们HPoly.
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;
Run Code Online (Sandbox Code Playgroud)