1

Say I have a type:

type A;
type XA is access constant A;
type A is
   record
      Member : Natural :=  1;
      Neighbor : XA;
   end record;

I'm confused about the access constant part. If I instantiate a instance of XA that points to a instance of A, what can I change while only holding the reference to the XA "instance" ?

Can I change the member of the object that XA points to ? I'd say no, but what about the Neighbor of the A in the XA object ?

Can someone explain the use of access constant to me ?

zython
  • 1,176
  • 4
  • 22
  • 50

1 Answers1

3

Here's a small example showing what it does:

procedure Access_Constant is

   type XA is access constant Integer;

   A : aliased Integer;
   X : XA;
begin
   X := A'Access;
   X.all := 4;
end Access_Constant;

When you attempt to compile it, the assignment to X works fine (X is a variable), while the assignment to X.all is forbidden (as X.all is a constant - per the definition of XA).

Although XA is declared to point to a constant Integer, even a variable is acceptable, but you can only treat a dereference of an XA entity as a constant Integer, even if the object is a variable.

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22
  • so just to get this straight, if a `access constant` appears on the left side, in any way, I cannot assignt it any value on the right side ? – zython May 28 '18 at 14:54
  • 1
    No. But you can't assign a value to a `constant`. If you try to compile the example, you will see that `X := A'Access` works fine. – Jacob Sparre Andersen May 28 '18 at 17:37
  • 1
    @zython I think it is more like `constant` applies to the right side. That is `access constant Integer;` should be read as "access to constant integer", not like "access constant to integer". – user7860670 May 31 '18 at 20:19
  • 1
    The other meaning is written `constant access`. – Jacob Sparre Andersen Jun 01 '18 at 04:34