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.

Conta<strong>in</strong>ers 261<br />

A deep copy of this structure:<br />

List orig<strong>in</strong>al, copy;<br />

copy := orig<strong>in</strong>al;<br />

-- Deep copy<br />

would give the memory layout as illustrated <strong>in</strong> Figure 17.6.<br />

Orig<strong>in</strong>al<br />

Red<br />

Green<br />

Blue<br />

Copy<br />

Red<br />

Green<br />

Blue<br />

Figure 17.6 Effect of the deep Copy := Orig<strong>in</strong>al;.<br />

A shallow copy :<br />

List orig<strong>in</strong>al, copy;<br />

copy := orig<strong>in</strong>al;<br />

-- Shallow copy<br />

would produce the memory layout as illustrated <strong>in</strong> Figure 17.7.<br />

Orig<strong>in</strong>al<br />

Red<br />

Green<br />

Blue<br />

Copy<br />

Figure 17.7 Effect of the shallow copy copy := orig<strong>in</strong>al;<br />

The major problem with the shallow copy is that serious errors will occur <strong>in</strong> a program if a change to the orig<strong>in</strong>al<br />

data structure is made. This is because the copy will change as well. Worse, if the storage for the Orig<strong>in</strong>al<br />

object is released, then the<br />

object Copy will be unsafe as it now po<strong>in</strong>ts to storage that is no longer considered active by the <strong>Ada</strong> run-time<br />

system.<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!