【问题标题】:crosswise implicit / explicit type conversation of records in pascal帕斯卡中记录的横向隐式/显式类型转换
【发布时间】:2021-03-07 08:21:32
【问题描述】:

我想在 pascal 中实现两种记录类型的交叉类型对话。但我收到错误“错误:预期的过程或功能”。这是相关代码:

{$mode objfpc}{$H+}
interface
{$modeswitch advancedrecords}
uses
  Classes, SysUtils;

type
  TRGBA8 = packed record
    r,
    g,
    b,
    a: byte;
    public
      constructor Create(r_in, g_in, b_in, a_in: byte);
  end;

  TRGBA16 = packed record
    r,
    g,
    b,
    a: word;
    public
      constructor Create(r_in, g_in, b_in, a_in: word);
      class operator Explicit(const c_in: TRGBA16): TRGBA8; inline;
      class operator :=(const c_in: TRGBA16): TRGBA8; inline;
  end;

  TRGBA8Helper = record helper for TRGBA8
    public
      class operator Explicit(const c_in: TRGBA8): TRGBA16;
  end;

我在声明 TRGBA8Helper 的 Operator Explicit 时收到错误消息。如何归档交叉类型的对话。提前致谢。

我正在使用来自 Ubuntu 存储库的 Lazarus 1.8.2 和 FPC 3.0.4。

【问题讨论】:

标签: operator-overloading record pascal helper freepascal


【解决方案1】:

我找到了解决方案。我的第一次尝试源自 FPC 源 (/usr/share/fpcsrc/3.0.4/rtl/objpas/types.pp) 的 TPoint 类。在 wiki.freepascal.org/Operator_overloading 中可以找到文档的链接:https://www.freepascal.org/docs-html/ref/refch15.html。此页面包含示例。所以我来到了这个解决方案:

{$mode objfpc}{$H+}
interface
{$modeswitch advancedrecords}
uses
  Classes, SysUtils;

type
  TRGBA8 = packed record
    r,
    g,
    b,
    a: byte;
    public
      constructor Create(r_in, g_in, b_in, a_in: byte);
  end;

  TRGBA16 = packed record
    r,
    g,
    b,
    a: word;
    public
      constructor Create(r_in, g_in, b_in, a_in: word);
  end;

operator Explicit(const c_in: TRGBA16): TRGBA8; inline;
operator :=(const c_in: TRGBA16): TRGBA8; inline;
operator Explicit(const c_in: TRGBA8): TRGBA16; inline;
operator :=(const c_in: TRGBA8): TRGBA16; inline;

所以不需要帮助记录,也不会发生错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2018-06-19
    相关资源
    最近更新 更多