Sessions on advanced C++ programming
Half-day seminar at CCC
Author: Zoltán Porkoláb
Budapest, 2007
A.2. References
Topics
- The concept behind reference
The concept behind reference is not easy to understand. In most
of the programming languages we use declaration to allocate
memory area and bind a name to that memory.
Declaration of a reference and initialization it to a memory field
is just a separation of this second action.
- Reference or value?
There are subtle differences between using value or
reference. One of the key difference is that pointers
are individual memory areas, while references are often not stored.
Other difference is the existence of null pointer, while references
have no way to express invalid target. We give a number of examples
when references are used and some when pointers are better.
- Parameter passing
Reference parameters and const reference parameters are typical
places when reference is used. There is a basic concept of
parameter passing in C++ which originate in the concept of
initialization. Initialization and assignment are
different concepts in C++.
- Return type
Return value of functions is another typical place where references
are used. However this situation differs from the parameter passing:
value return types and const reference are not
logically interchangeble. We must learn where const ref is an
efficiency improvement, and where is a mistake.