next up previous contents
Next: ``.'' vs. ``->'' Up: Some basics of object-oriented Previous: Methods; Inlining   Contents

References (``&'') in subroutine calls

C and C++ by default call subroutine arguments ``by value'', which means that they copy the complete object. For example,


{}
void doSomething( Nodes nodes ) {
...
}
...
   doSomething( theNodes ) ;
...
would copy the whole Nodes data structure and then operate on that copy. That has two often undesired or unexpected side-effects: This behavior can be avoided when references are used, as follows:


{}
void doSomething( Nodes& nodes ) {
...
}
...
   doSomething( theNodes ) ;
...
Note the ``&'' in the argument list. The result of this is that doSomething will directly use the already existing nodes data structure.

In general, we will always use references in subroutine calls. Only when we pass int or double will we, wenn we do not want to pass back a result, omit the ``&''.

References can also be used in other contexts, in particular to avoid pointers to objects (see below). We will not use them for that since we find the pointer version easier to understand for non-experts.


next up previous contents
Next: ``.'' vs. ``->'' Up: Some basics of object-oriented Previous: Methods; Inlining   Contents
2004-02-02