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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

216 Polymorphism<br />

15.3.1 Summary: access all, access constant, access<br />

The follow<strong>in</strong>g table summarizes the choice and restrictions that apply to the use of access values.<br />

Note Declaration (T is an Integer type) Example of use<br />

1 type P_T is access all T; a_pt := a_t'Access;<br />

a_t : aliased T; a_pt.all := 2;<br />

a_pt: P_T;<br />

2 type P_T is access constant T;<br />

a_t : aliased constant T := 0;<br />

a_pt: P_T;<br />

3 type P_T is access T;<br />

a_pt: P_T;<br />

a_pt := a_t'Access;<br />

Put( a_pt.all );<br />

a_pt := new T;<br />

a_pt.all := 2;<br />

Note 1: Used when it is required to have both read and write access to a_t us<strong>in</strong>g the access value held <strong>in</strong><br />

a_pt. The storage described by a_t may also be dynamically created us<strong>in</strong>g an allocator.<br />

Note 2: Used when it is required to have only read access to a_t us<strong>in</strong>g the access value held <strong>in</strong> a_pt. The<br />

storage described by a_t may also be dynamically created us<strong>in</strong>g an allocator.<br />

Note 3: Used when the storage for an <strong>in</strong>stance of a T is allocated dynamically. Access to an <strong>in</strong>stance of T can<br />

be read or written to us<strong>in</strong>g the access value obta<strong>in</strong>ed from new.<br />

This form may only be used when an access value is created with an allocator (new T).<br />

15.4 Use of dynamic storage<br />

The Stack package shown <strong>in</strong> Section 14.3 could be rewritten us<strong>in</strong>g dynamic storage allocation. In rewrit<strong>in</strong>g this<br />

package, the user <strong>in</strong>terface to the package has not been changed. Thus, a user of this package would not need to<br />

modify their program.<br />

There is however, one important difference and this is that as the implementation of the stack uses l<strong>in</strong>ked<br />

storage as the implementation stands it cannot be correctly copied. To prevent a user of the package from<br />

attempt<strong>in</strong>g to copy an <strong>in</strong>stance of a stack the type Stack is created as a limited type.<br />

generic<br />

type Stack_Element is private; --<br />

package Class_Stack is<br />

type Stack is limited private;<br />

Stack_Error : exception;<br />

--NO copy<strong>in</strong>g<br />

procedure Push( The:<strong>in</strong> out Stack; Item:<strong>in</strong> Stack_Element );<br />

procedure Pop(The:<strong>in</strong> out Stack; Item :out Stack_Element );<br />

procedure Reset( The:<strong>in</strong> out Stack );<br />

private<br />

type Node;<br />

type P_Node is access Node;<br />

pragma Controlled( P_Node );<br />

type Node is record<br />

Item : Stack_Element;<br />

P_Next : P_Node;<br />

end record;<br />

--Mutually recursive def<br />

--Po<strong>in</strong>ter to a Node<br />

--We do deallocation<br />

--Node holds the data<br />

--The stored item<br />

--Next <strong>in</strong> list<br />

type Stack is record<br />

P_Head : P_Node := null;<br />

end record;<br />

end Class_Stack;<br />

--First node <strong>in</strong> list<br />

Note:<br />

The compiler directive pragma Controlled( P_Node ) to <strong>in</strong>form the compiler that the<br />

programmer will explicitly perform the storage de-allocation for data allocated with the type P_Node.<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!