18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dynamic memory allocation 211<br />

In a program it is usual to deal with the contents or rvalue of an object. The access value of an object is its<br />

lvalue. For example, after the follow<strong>in</strong>g fragment of code has been executed:<br />

declare<br />

type P_Integer is access all Integer;<br />

type P_P_Integer is access all P_Integer;<br />

P_P_People : P_P_Integer;<br />

P_People : aliased P_Integer;<br />

People : aliased Integer;<br />

beg<strong>in</strong><br />

People := 42;<br />

P_People := People'Access;<br />

P_P_People := P_People'Access;<br />

end;<br />

the follow<strong>in</strong>g expressions will deliver the contents of the object people:<br />

Expression<br />

People<br />

Diagram<br />

People<br />

24<br />

P_People.all P_People People<br />

24<br />

P_P_People.all.all P_P_People P_People People<br />

24<br />

In a similar way the follow<strong>in</strong>g statements will assign 42 to the object people.<br />

Statement<br />

Explanation<br />

People := 42;<br />

Straight-forward assignment.<br />

P_People.all := 42;<br />

S<strong>in</strong>gle level of <strong>in</strong>direction.<br />

P_P_People.all.all := 42; Double level of <strong>in</strong>direction.<br />

15.1.3 Read only access<br />

Access to an object via an access value may be restricted to read only by replac<strong>in</strong>g all with constant <strong>in</strong> the<br />

declaration of the access type. For example, <strong>in</strong> the follow<strong>in</strong>g fragment of code, only read access is allowed to<br />

People when us<strong>in</strong>g the access value held <strong>in</strong> P_People.<br />

declare<br />

type P_Integer is access constant Integer;<br />

People : aliased Integer;<br />

P_People : P_Integer;<br />

beg<strong>in</strong><br />

People := 24;<br />

P_People := People'Access; --Access value for people<br />

Put("The number of people is : "); Put( P_People.all );<br />

New_L<strong>in</strong>e;<br />

end;<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!