References

A reference is an alias to an object. That is, it is another name for the object, and so access to the object is the same through a reference as it is through the object's variable name. A reference is declared using a & symbol on the reference name and it is initialized and accessed in exactly the same way as a variable:

    int i = 42;     int *pi = &i;  // pointer to an integer     int& ri1 = i;  // reference to a variable     i = 99;        // change the integer thru the variable     *pi = 101;     // change the integer thru the pointer     ri1 = -1;      // change the integer thru the reference     int& ri2 {i};  // another reference to the variable     int j = 1000;     pi = &j;       // point to another integer

In this code, a variable is declared and initialized, then a ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.