将数组初始化为null对象

时间:2015-09-05 12:25:56

标签: arrays ada

我说有一个Employee包和一个Office包。 Office包具有一组Employee对象。我可以声明数组,以便

officeArray : Office.Vector(1..20);

但是如何将officeArray初始化为一组20个空对象?我试过了

officeArray := (others => null);

这不起作用。编译器说它需要Employee对象。我可以创建一个虚拟的Employee对象来填充数组,还是有另一种方法可以做到这一点?

1 个答案:

答案 0 :(得分:4)

以下是一个示例程序:

With Ada.Text_IO; Use Ada.Text_IO;  
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;

procedure Program is

  type Employee is record
      name : String(1..50);
      end record;

  type EmployeeArr is array (Positive range <>) of Employee;

  type EmployeePtr is access all Employee;

  type EmployeePtrArr is array (Positive range <>) of EmployeePtr;

  employees1 : EmployeeArr(1..20);
  employees2 : EmployeePtrArr(1..20);

begin
  employees1 := (others => null); -- this will NOT compile
  employees2 := (others => null); -- this compiles fine
end Program;

为了分配(others => null),数组的元素类型必须是访问类型。