03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

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.

52 CHAPTER 2. <strong>C++</strong> BASICS<br />

Encapsulate your dynamic memory management in classes. Then you have to deal with it<br />

only once per class. 21 If all memory allocated by an object is released when the object<br />

is destroyed then it does not matter how many memory you allocate. If you have 738<br />

objects with dynamic memory then it will be released 738 times. If you have called new<br />

738 times, partly in loops and branches, can you be sure that you have called delete 738<br />

times? We know that there are tools <strong>for</strong> this but these are errors you better prevent than<br />

fix. Even with the encapsulation there is probably something to fix inside the classes but<br />

this is orders of magnitude less work than having pointers spread all over your program.<br />

We have shown two main purposes of pointers:<br />

• Dynamic memory management; and<br />

• Referring other objects.<br />

For the <strong>for</strong>mer there is no alternative to pointers, dynamic memory handling needs pointers,<br />

either directly or using classes that contain pointers. To refer to other objects, there exist<br />

another kind of types called reference (surprise, surprise) that we will introduce in the next<br />

section.<br />

2.10.2 References<br />

The following code introduces a reference:<br />

int i= 5;<br />

int& j= i;<br />

j= 4;<br />

std::cout ≪ ”j = ” ≪ j ≪ ’\n’;<br />

The variable j is referring to i. Changing j will also alter i and vice versa, as in the example. i<br />

and j will always have the same value. One can think of a reference as an alias. Whenever one<br />

defines a reference, one must directly say what it is referring to (other then pointers). It is not<br />

possible to refer to another variable later.<br />

So far, that does not sound extremely useful. References are extremely useful <strong>for</strong> function<br />

arguments (§ 2.6), <strong>for</strong> refering parts of other objects (e.g. the seventh entry of a vector), and<br />

<strong>for</strong> building views ( 22 ).<br />

2.10.3 Comparison between pointers and references<br />

The advantage of pointers over references is the ability of dynamic memory management and<br />

address calculation. On the other hand, references refer to defined locations 23 , they always<br />

must refer to something, they do not leave memory leaks (unless you play really evil tricks),<br />

and they have the same notation in usage as the referred object.<br />

21<br />

It is save to assume that there are many more objects than classes; otherwise there is something wrong with<br />

the program.<br />

22<br />

TODO: reref to a section when it is written<br />

23<br />

References can refer to arbitrary addresses but one must work hard to achieve this. For your own savefy we<br />

will not show you how to make reference to behave as badly as pointers.

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

Saved successfully!

Ooh no, something went wrong!