当前位置:  开发笔记 > 开发工具 > 正文

阿达:如何解决"循环单位依赖"?

如何解决《阿达:如何解决"循环单位依赖"?》经验,为你挑选了1个好方法。

假设我有两个记录:动物.每条记录都在一个单独的包中.

套餐人员:

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;

我在这里有循环单元依赖,编译器会产生致命错误.

有没有人有解决这个问题的模式?

谢谢!



1> Simon Wright..:

你需要limited with,这是为了解决这个问题而引入的.请参阅Ada 2005的基本原理,第4.2节.

Animals并且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;

演示程序具有的全貌AnimalsPersons.这个例子非常笨拙; 您可以通过向Animals和添加子程序来更好地组织事物Persons.需要注意的是,身体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

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有