假设我有两条记录:Person and Animal。每条记录都在一个单独的包中。
包人:
with animals;
use animals;
package persons is
type person is record
...
animalref: animalPOINTER;
...
end record;
type personPOINTER is access person;
end persons;
包装动物:
with persons;
use persons;
package animals is
type animal is record
...
ownerref: personPOINTER;
...
end record;
type animalPOINTER is access animal;
end animals;
我这里有循环单元依赖,编译器产生致命错误。
有人有解决此类问题的模式吗?
Thanks!
你需要limited with
,它的引入正是为了解决这个问题。请参阅Ada 2005 第 4.2 节的基本原理 http://www.adaic.org/resources/add_content/standards/05rat/html/Rat-4-2.html.
Animals
and Persons
是对称的(我的编辑器调整了布局和大小写;我为每个组件添加了一个记录组件,以便下面的演示程序可以打印一些内容):
limited with Animals;
package Persons is
-- One of the few things you can do with an incomplete type, which
-- is what Animals.Animal is in the limited view of Animals, is to
-- declare an access to it.
type AnimalPOINTER is access Animals.Animal;
type Person is record
Name : Character;
Animalref : AnimalPOINTER;
end record;
end Persons;
limited with Persons;
package Animals is
type PersonPOINTER is access Persons.Person;
type Animal is record
Name : Character;
Ownerref : PersonPOINTER;
end record;
end Animals;
演示程序具有完整的视图Animals
and Persons
。这个例子相当笨拙;您可以通过添加子程序来更好地组织事情Animals
and Persons
。请注意,body of Animals
可以(并且必须)with Persons;
如果需要使用任何东西Persons
.
with Ada.Text_IO; use Ada.Text_IO;
with Animals;
with Persons;
procedure Animals_And_Persons is
A : Persons.animalPOINTER := new Animals.Animal;
P : Animals.PersonPOINTER := new Persons.Person;
begin
A.all := (Name => 'a', Ownerref => P);
P.all := (Name => 'p', Animalref => A);
Put_Line (P.Name & " owns " & P.Animalref.Name);
Put_Line (A.Name & " is owned by " & A.Ownerref.Name);
end Animals_And_Persons;
编译并运行时给出
$ ./animals_and_persons
p owns a
a is owned by p
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)