首页 > 解决方案 > Pascal - 如何从该类中的函数返回一个类的数组?

问题描述

如果我有一个类,它具有一个受保护的属性,该属性是该类的一个数组和一个公共函数来获取该数组并返回它 - 我如何声明它以便它允许该数组作为返回值?

通常我会使用

TNodeArray = array of Node

方法,但这在这里行不通。这就是我正在尝试的:

Node = class
  protected
     Neighbours : array of Node;
  public
     function GetNeighbours() : array of Node; //This is the problem line
end;

任何帮助都感激不尽!谢谢!

标签: arraysfunctionclassreturnpascal

解决方案


使用数组类型作为参数或函数结果值的方法是使用 distinct 类型声明:

TNodeArray

在这里,您还必须转发声明Node类以解析循环引用。

Type

  Node = class;  // Forward declaration of the class

  TNodeArray = array of Node;

  Node = class
    protected
     Neighbours : TNodeArray;
    public
     function GetNeighbours() : TNodeArray; 
  end;

推荐阅读