首页 > 解决方案 > 带有 objfpc 模式记录的 TFPGList

问题描述

我想使用带有自定义记录的 TFPGList。我花了很长时间从互联网上获取所有必要的提示来编译这个小片段:

program Project1;
{$mode delphi}{$H+}
uses
  fgl;

type    
  TSomeRecord = record
    feld_1: Byte;
    class operator Equal(Left, Right : TSomeRecord) Result : Boolean;
  end;

  class operator TSomeRecord.Equal (Left, Right: TSomeRecord) Result:  Boolean;
  begin
    Result := Left.feld_1 = Right.feld_1;
  end;

type
  TypedList = TFPGList<TSomeRecord>;

var
  x : TypedList;

begin
end.    

如您所见,问题在于为记录指定 Equal 运算符。此外,这似乎仅在德尔福模式下才有可能。

假设我不想在 delphi 模式下编写这个程序,而是在 objfpc 模式下: 为记录指定 Equal 运算符的正确语法是什么?是否可以?

我的fpc版本是3.0.4

标签: freepascal

解决方案


(* Please try the following compiled with Lazarus 2.06, FPC 3.04: *)  
unit..
..
{$IFDEF fpc} {$MODESWITCH AdvancedRecords+} {$ENDIF} 
..
interface
..
type    
    TSomeRecord = record
        feld_1: Byte;
        class operator = (Left, Right : TSomeRecord): Boolean;
    end;
..
implementation
..
class operator TSomeRecord .= (Left, Right : TSomeRecord): Boolean;
begin
    Result:=Left.feld_1 = Right.feld_1;
end;
..

推荐阅读