C++ References let you give variables alternative names. For example, if you write
int & jones = flossie;
then "jones" becomes an alternative name for flossie in the current scope. If you assign something new to jones, you're not going to make the variable into an alternative name for something else - you're actually going to change the value that's being held under (both) names. You can see a worked example in
[source code here].
Why do you want to give variables alternative names in this way?
One reason is that you're wanting to loop through every member of an array - perhaps an array of objects too, where the variable names to describe a memory location are complex. Rather than repeat the complexity time after time in your code, a reference is a lighweight alternative to a pointer to simplify it. For example, you might write:
for (int i=1; i<length; i++) {
Shape * ¤t = candidates[i];
sofar = current->heavier(sofar);
and you can then keep making further references to
current rather than the more complex
candidates[i] for the rest of the loop too.
There's a further example that compares and contrasts references and pointers
[here], and one that uses both reference and pointers to array members
[here] (written 2010-07-02)
Associated topics are indexed under
C234 - C and C based languages - Further C++ Object Oriented features [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3057] Lots of things to do with and within a C++ class - (2010-11-16)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2576] What does const mean? C and C++ - (2010-01-15)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2004] Variable Scope in C++ - (2009-01-22)
[1819] Calling base class constructors - (2008-10-03)
[1159] It can take more that one plus one to get two. - (2007-04-22)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[802] undefined reference to typeinfo - C++ error message - (2006-07-15)
[801] Simple polymorphism example - C++ - (2006-07-14)
Some other Articles
Transport in Wessex - today and into the futureAtworth Village Fete - on the Melksham Community Area Partnership standFurther C++ material - view new or oldFresh Paint for course presentation - including C++What are C++ references? Why use them?C course - final course example puts it all togetherLocal information for Melksham Hotel GuestsCatching up with an old friendObjects and Inheritance in C++ - an easy startLearning about Regular Expressions in C through examples