TFPGList以objfpc模式记录

问题描述 投票:0回答:1

我想使用带有自定义记录的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模式下可行。

假设我不想以delphi模式而是以objfpc模式编写该程序:将Equal运算符指定给记录的正确语法是什么?有可能吗?

我的fpc版本是3.0.4

freepascal
1个回答
0
投票
(* 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;
..
© www.soinside.com 2019 - 2024. All rights reserved.