064.访问静态变量---指向常量的指针

Ada 95 has the ability to access a local or global variable with an access variable. This was not permitted with Ada 83. In order to do so, it is necessary to identify the access variable as one with the ability to access a local or global variable by using the keyword all as is done in lines 13 through 15 of the example program.
with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO;

procedure AcessAll is

   type BRICK_TYPE is record
      Length : INTEGER;
      Width  : INTEGER;
      Height : INTEGER;
   end record;

   type ACCESS_INT   is access all INTEGER;
   type ACCESS_FLT   is access all FLOAT;
   type ACCESS_BRICK is access all BRICK_TYPE;

   Number   : INTEGER            := 27;
   Count    : aliased INTEGER    := 12;
   Pt_Count : ACCESS_INT;

   Size     : aliased FLOAT      := 7.4;
   Pt_Size  : ACCESS_FLT;

   Brick    : aliased BRICK_TYPE := (3, 4, 6);
   Pt_Brick : ACCESS_BRICK;

begin

   Pt_Count := Count'Access;
-- Pt_Count := Number'Access;  Illegal - Number not aliased
-- Pt_Count := Size'Access;    Illegal - Size is FLOAT
-- Pt_Count := Brick'Access;   Illegal - Brick is BRICK_TYPE
   Pt_Count.All := Pt_Count.All + 13;
   Put("The value of Count is now ");
   Put(Count, 5);
   New_Line;

   Pt_Size := Size'Access;
   Put("The value of Size is now ");
   Put(Pt_Size.All, 5, 2, 0);
   New_Line;

   Pt_Brick := Brick'Access;
   Put("The brick measures  ");
   Put(Pt_Brick.Length, 5);   -- Using the pointer
   Put(Pt_Brick.Width, 5);     -- Using the pointer
   Put(Brick.Height, 5);       -- Using the object
   New_Line;

end AcessAll;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值