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.

15.7 Attributes 'Access and 'Unchecked_Access<br />

Dynamic memory allocation 225<br />

The access value of an object may only be taken if the object is declared at the same lexical level or lower than the<br />

type declaration for the access value. If it is not, then a compile time error message will be generated when an<br />

attempt is made to take the object’s access value. This is to prevent the possibility of hold<strong>in</strong>g an access value for<br />

an object which does not exist. For example, <strong>in</strong> the follow<strong>in</strong>g program:<br />

procedure Ma<strong>in</strong> is<br />

Max_Chs : constant := 7;<br />

type Height_Cm is range 0 .. 300;<br />

type Person is record<br />

Name : Str<strong>in</strong>g( 1 .. Max_Chs ); --Name as a Str<strong>in</strong>g<br />

Height : Height_Cm := 0; --Height <strong>in</strong> cm.<br />

end record;<br />

Mike : aliased Person := Person'("Mike ",156);<br />

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

declare<br />

type P_Person is access all Person; --Access type<br />

P_Human: P_Person;<br />

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

P_Human:= Mike'access; --OK<br />

declare<br />

Clive : aliased Person := Person'("Clive ", 160 );<br />

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

P_Human := Clive'Access;<br />

end;<br />

Put( P_Human.Name ); New_L<strong>in</strong>e; --Clive no loner exists<br />

P_Human := Mike'access;<br />

end;<br />

end Ma<strong>in</strong>;<br />

a compile time error message is generated for the l<strong>in</strong>e:<br />

--Change to Mike<br />

P_Human := Clive'Access;<br />

-- Compile time error<br />

as the object Clive does not exist for all the scope of the type P_Person. In fact, there is a serious error <strong>in</strong> the<br />

program, as when the l<strong>in</strong>e:<br />

Put( P_Human.Name ); New_L<strong>in</strong>e;<br />

-- Clive no longer exists<br />

is executed, the storage that p_human po<strong>in</strong>ts to does not exist. Remember the scope of clive is the declare<br />

block.<br />

In some circumstances the access value of an object declared <strong>in</strong> an <strong>in</strong>ner block to the access type declaration is<br />

required. If this is so, then the compiler check<strong>in</strong>g can be subverted or overridden by the use of<br />

'Unchecked_Access. Thus, the follow<strong>in</strong>g code can be written:<br />

procedure Ma<strong>in</strong> is<br />

-- Declaration of Person, P_Person, Mike etc.<br />

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

P_Human:= Mike'Access;<br />

-- OK<br />

declare<br />

Clive : aliased Person := Person'("Clive ", 160 );<br />

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

P_Human := Clive'Unchecked_Access;<br />

Put( P_Human.Name ); New_L<strong>in</strong>e; -- Clive<br />

end;<br />

P_Human:= Mike'Access;<br />

--Change to Mike<br />

Put( P_Human.Name ); New_L<strong>in</strong>e; --Mike<br />

end Ma<strong>in</strong>;<br />

Of course the compiler can no longer help the programmer <strong>in</strong> detect<strong>in</strong>g possible <strong>in</strong>constancies.<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!