Understanding reference basics
In this section, we will revisit reference basics as well as introduce operators applicable to references, such as the reference operator &
. We will employ the reference operator (&
) to establish a reference to the existing variable. Like pointer variables, reference variables refer to memory that is defined elsewhere.
Using reference variables allows us to use a more straightforward notation than the notation that pointers use when using indirectly accessed memory. Many programmers appreciate the clarity in the notation of a reference versus a pointer variable. But, behind the scenes, memory must always be properly allocated and released; some portion of memory that is referenced may come from the heap. The programmer will undoubtedly need to deal with pointers for some portion of their overall code.
We will discern when references and pointers are interchangeable, and when they are not. Let’s get started with the basic notation...