【发布时间】:2018-08-26 17:18:35
【问题描述】:
我有两个类,一个子类:
type MyChildClass = class
public
parent: ^MyParent;
end;
还有一个父类:
type MyParentClass = class
public
childs: array of ^MyChildClass;
end;
但是,这不起作用,因为只有最后一个声明的类知道另一个。示例:
program Test;
interface
type MyChildClass = class
public
parent: ^MyParentClass;
end;
type MyParentClass = class
public
childs: array of ^MyChildClass;
end;
implementation
end.
这不会编译,因为第 7 行将抛出错误“未声明的标识符 'MyParentClass' 符合预期。使用抽象类只能部分解决问题。我真的很难找到解决方案。也许使用接口会有所帮助?
【问题讨论】:
-
Forward declaration 是您所缺少的。除此之外,您可以声明那些不像指针的字段类型(通过删除那些
^运算符)。我们使用T前缀作为Type 声明。 -
非常感谢!就是这样!
-
不客气!
-
FWIW,不要对父级或子级使用指针语法。在 Delphi 中,类实例变量已经是引用。在我关于指针(和引用)的文章中了解更多信息:Addressing pointers -- References。
-
@David:我的意思是在这种情况下,因此提到了父母和孩子。所以我认为我写的是正确的。
标签: oop pascal paradigms delphi