next up previous
Next: Functions Up: Online C++ FAQ/Tutorial and Previous: How do I use

How do I use references?

A reference is merely an initialized pointer. This reduces significantly zero pointer and un-initialized pointers errors. Prefer references to pointers. Although the two following codes look equivalent, the second implementation prevents invalid compilation.
// in C                     // in C++

int     i;                  int	   i;
int     *pi = &i;           int    &ti = i;
int     *pj;                int    &rj; // g++ refuses to compile that
                         
*pi = 0;                    ri = 0;
*pj = 0; // segfault        rj = 0;



Alexis Angelidis (PhD) 2005-01-11