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.

Dynamic memory allocation 223<br />

15.5.2 Hidden vs. visible storage <strong>in</strong> a class<br />

The ma<strong>in</strong> benefit of this approach is that a client of the class does not need to recompile their code when the<br />

storage structure of the class is changed. The client code only needs to be rel<strong>in</strong>ked with the new implementation<br />

code. This would usually occur when a new improved class library is provided by a software supplier. Naturally,<br />

this assumes that the <strong>in</strong>terface with the library stays the same.<br />

The pros and cons of the two approaches are:<br />

Criteria Hidden storage Visible storage<br />

Compilation efficiency Fewer resources required,<br />

as only a recompile of the<br />

class and then a re-l<strong>in</strong>k<br />

need to be performed.<br />

Greater as all units that use<br />

the class need to be<br />

recompiled.<br />

Run-time efficiency<br />

Client access to data<br />

components of an object.<br />

Worse as there is the<br />

dynamic storage allocation<br />

overhead.<br />

None<br />

No extra run-time<br />

overhead.<br />

None<br />

Note:<br />

The extra cost of re-compil<strong>in</strong>g and re-l<strong>in</strong>k<strong>in</strong>g all units of a program may be marg<strong>in</strong>al when compared<br />

with just re-l<strong>in</strong>k<strong>in</strong>g.<br />

15.6 Access value of a function<br />

The access value of a function or procedure may also be taken. This allows a function or procedure to be passed as<br />

a parameter to another function or procedure. For example, the procedure apply applies the function passed as a<br />

parameter to all elements of the array. The implementation of this function is shown <strong>in</strong> the fragment of code<br />

below:<br />

type P_Fun is access function(Item:<strong>in</strong> Float) return Float;<br />

type Vector is array ( Integer range ) of Float;<br />

procedure Apply( F:<strong>in</strong> P_Fun; To:<strong>in</strong> out Vector ) is<br />

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

for I <strong>in</strong> To'range loop<br />

To(I) := F( To(I) );<br />

end loop;<br />

end Apply;<br />

Note:<br />

The de-referenc<strong>in</strong>g is done automatically when the function f is called. This could have been done<br />

explicitly with F.all( To(I) ). This explicit de-referenc<strong>in</strong>g is, however, required if the called<br />

function or procedure has no parameters.<br />

The first parameter to the procedure apply can be any function which has the signature:<br />

function(Item:<strong>in</strong> Float) return Float;<br />

Two such functions are:<br />

function Square( F:<strong>in</strong> Float ) return Float is<br />

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

return F * F;<br />

end Square;<br />

function Cube( F:<strong>in</strong> Float ) return Float is<br />

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

return F * F * F;<br />

end Cube;<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!